结束场景后需要启动新场景

问题描述 投票:0回答:2

我正在为孩子们做一个教育游戏..但我在场景结束时停止了我无法创建一个代码来启动新场景..在第一个脚本中,当玩游戏时场景直到最后一个场景才停止。

YouWin.pictureInPlace++;

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

第一个脚本(经理)

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

    }
}
unity3d boolean 2d unityscript scene2d
2个回答
1
投票

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

public GameObject emptyObject;

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

0
投票

毕竟感谢帮助我,但我能够在第一个脚本(经理)中找到正确的解决方案:

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

    }

这是正确的解决方案;

© www.soinside.com 2019 - 2024. All rights reserved.