【问题标题】:After ending the scene need to start new scene结束场景后需要开始新场景
【发布时间】:2019-07-19 09:21:16
【问题描述】:

我正在为孩子们做一个教育游戏.. 但我在场景的尽头停了下来 我无法编写代码来开始新场景.. 在第一个脚本中 玩游戏时,直到最后一个场景才会停止。

YouWin.pictureInPlace++;

我搜索了很多,没有找到我的问题,所以我咨询了你。做一个按钮去下一个场景更容易,但我更喜欢自动做 我认为这个任务可以通过布尔值来完成,但它需要引用游戏对象......以及 2 个图像上的脚本。 第一个脚本(经理)在 Canvas 上放了四个图像。 第二个(YouWin)我把空的游戏对象.. 感谢您的帮助

第一个脚本(Manger)

using UnityEngine;
using UnityEngine.EventSystems;

public class Manager : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    Vector2 pos1;
    public GameObject pos2;
    private bool canMove;
    public GameObject winner;
    void Start()
    {
        pos1 = transform.position;
        canMove = true;
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        Debug.Log(eventData);
    }

    public void OnDrag(PointerEventData eventData)
    {
        if (canMove)
            transform.position = Input.mousePosition;       
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        float distance = Vector3.Distance(transform.position, pos2.transform.position);
        if (distance < 50)
        {
            transform.position = pos2.transform.position;
            transform.localScale = pos2.transform.localScale;
            canMove = false;
            winner.GetComponent<YouWin>().pictureInPlace++;   
        }
        else
        {
            transform.position = pos1;
        }
    }  
}

第二个脚本(YouWin)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class YouWin : MonoBehaviour
{
public int NumberOfImages;
public int pictureInPlace;
public string sceneName;

    void Update()
    {
        if (pictureInPlace == NumberOfImages)
        {
            StartCoroutine(LoadScene());
            Debug.Log("You Win!");
        }
    }

    IEnumerator LoadScene()
    {
       yield return new WaitForSeconds(1.5f);
        SceneManager.LoadScene(sceneName);

    }
}

【问题讨论】:

  • async 是一个关键字,因此您不能调用变量async ...您实际上应该从AsyncOperation async = ... 得到一个编译器错误。如果你从不使用它,你将它存储在变量中是为了什么?只需致电SceneManager.LoadSceneAsync(sceneName); 为什么pictureInPlace 是静态的而NumberOfImages 不是?你得到Debug.Log“你赢了”吗?
  • 这是我得到我的代码的教程,但我修改了它link 并且代码工作正常没有错误没有错误直到现在但是关于SceneManager.LoadSceneAsync(sceneName);AsyncOperation async = ... 这不是问题即时只是尝试代码,但我需要在场景中匹配图像后开始下一个场景即使是取消当前代码并在YouWin 脚本中创建一个新代码

标签: c# unity3d boolean 2d scene2d


【解决方案1】:

感谢您帮助我,但我找到了正确的解决方案 只需在第一个脚本(经理)中制作:

bool static Done;
void Start()
    {
bool Done = false;
    }
 public void OnEndDrag(PointerEventData eventData)
    {
        float distance = Vector3.Distance(transform.position, pos2.transform.position);
        if (distance < 50)
        {
            transform.position = pos2.transform.position;
            transform.localScale = pos2.transform.localScale;
            canMove = false;
            bool Done = True; 
        }
}

然后从第二个脚本 (YouWin) 中调用它

public string sceneName;
void Update()
{
if(Youwin.Done)
{
 StartCoroutine(LoadScene());
}
}

IEnumerator LoadScene()
    {

        yield return new WaitForSeconds(0.5f);
        SceneManager.LoadScene(sceneName);

    }

这是正确的解决方案;

【讨论】:

    【解决方案2】:

    您的 Manager 脚本中似乎没有引用 YouWin。您应该通过将其添加为全局变量 public YouWin &lt;variable_name&gt;; 或通过引用您的空 GameObject 并获取 YouWin 组件来包含它:

    public GameObject emptyObject;
    
    emptyObject.GetComponent<YouWin>().picturesInPlace++;
    

    【讨论】:

    • 我正在尝试制作布尔值来制作 At Manager 脚本,例如:'public bool whenlDone;当lDone =真; ' 但即使我在 youWin 脚本中引用它也没有发生
    • @Mohamedessam 请避免使用粗体!我已经编辑了您的答案,但也避免在 cmets 中使用它.. 用于强调某些关键字而不是在整个段落中使用它。
    • @Savaria pictureInPlace静态的,因此YouWin.pictureInPlace++; 的调用有效
    • 但他只是调用 YouWin 而不引用脚本或提取组件。因此,YouWin 只是一个未声明的变量,没有属性pictureInPlace
    • @Savaria YouWinYouWin 本身的类......再次:pictureInPlacestatic,因此属于 YouWin 本身的类,而不是实例跨度>
    猜你喜欢
    • 1970-01-01
    • 2018-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多