繁体   English   中英

具有统一时间和场景管理器的 Unity Bug

[英]Unity Bug with unity time and scene manager

我有一个问题,我的游戏中有三个场景,商店,主菜单和游戏。 在游戏中,我有一个每秒生成敌人的脚本。 所以我的问题是,如果我在主菜单或商店等待 20 秒,然后 go 进入游戏,它会一次产生 20 个敌人(脚本在空的游戏对象中),我尝试了很多修复。 这是我最新的生成敌人脚本:

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

public class SpawnScript : MonoBehaviour
{
    public Transform[] spawnPoints;

    public GameObject[] enemyPrefabs;

    public float nextActionTime;

    public float period;

    private bool startScript = false;
    void Start()
    {
        Scene currentScene = SceneManager.GetActiveScene();

        string sceneName = currentScene.name;

        if (sceneName == "Game")
        {
            nextActionTime = 0.0f;
            period = 1f;
            startScript = true;
        }
    }
    void Update()
    {
        if (startScript != false)
        {
            if (Player.isDead != true)
            {
                if (Time.time > nextActionTime)
                {
                    nextActionTime += period;
                    int spawnArrayKey = Random.Range(0, 4);
                    int enemyId = Random.Range(0, 3);
                    Instantiate(enemyPrefabs[enemyId], spawnPoints[spawnArrayKey].position, spawnPoints[spawnArrayKey].rotation);
                }
            }
        }
    }
}

我也尝试将void Start()更改为void Awake() ,仍然不起作用,这是非常简单的主菜单脚本

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

public class Buttons : MonoBehaviour
{
    public void PlayGame()
    {
        SceneManager.LoadScene("Game");
    }
    public void GoToShop()
    {
        SceneManager.LoadScene("Shop");
    }
}

这也是我的构建经理在此处输入图像描述

我找到了答案,我的脚本中有Time.time ,我已将其设置为Time.timeSinceLevelLoad现在它可以工作了

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM