簡體   English   中英

結束場景后需要開始新場景

[英]After ending the scene need to start new scene

我正在為孩子們做一個教育游戲..但是我在場景結束時停下來我無法編寫代碼來開始新場景..在玩游戲時的第一個腳本中,場景直到最后一個場景才停止。

YouWin.pictureInPlace++;

搜索了很多,沒有找到我的問題,所以咨詢了你。 做一個按鈕去下一個場景更容易,但我更喜歡自動完成我認為這個任務可以通過布爾值來完成,但它需要引用游戲對象..和 2 個圖像上的腳本。 第一個腳本(管理器)在畫布上放了四張圖片..第二個(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);

    }
}

您似乎沒有在Manager腳本中引用YouWin 您應該通過將其添加為全局變量public YouWin <variable_name>;來包含它public YouWin <variable_name>; 或者通過引用您的空GameObject並獲取YouWin組件:

public GameObject emptyObject;

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

畢竟感謝幫助我,但我能夠找到正確的解決方案,只需在第一個腳本(管理器)中創建:

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);

    }

這是正確的解決方案;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM