C# Unity Bug与Unity time和场景管理器

C# Unity Bug与Unity time和场景管理器,c#,unity3d,C#,Unity3d,我有一个问题,我的游戏中有三个场景,商店,主菜单和游戏。在游戏中,我有一个每秒钟都会产生敌人的脚本。所以我的问题是,如果我在主菜单或商店等待20秒,然后进入游戏,它会一次产生20个敌人(脚本在空的游戏对象中),我尝试了很多修复。这是我最新的繁殖敌人脚本: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public

我有一个问题,我的游戏中有三个场景,商店,主菜单和游戏。在游戏中,我有一个每秒钟都会产生敌人的脚本。所以我的问题是,如果我在主菜单或商店等待20秒,然后进入游戏,它会一次产生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.timesInceslevelLoad
,现在它可以工作了我找到了答案,我在我的脚本中设置了
Time.Time
,现在它可以工作了。也许当你从菜单或任何你需要重置下一个动作时间的地方回来时,它不是不可调整的。也许当你从菜单或任何你需要重置下一个动作的时候回来