Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

286
Vistas
Unity Bug with unity time and scene manager

I have a problem, I have three scenes in my game, shop, main menu, and game. In game I have a script that spawns enemies every second. So my problem is if I wait 20 seconds in the main menu or shop, and go to the game, it will spawn 20 enemies at one time(script is in the empty game object), I tried a lot of fixes. That is my latest spawn enemy script:

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

I also tried to change void Start() to void Awake(), still don't work, here is the main menu script which is very simple

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

Also here is my build manager enter image description here

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I found answer, I had Time.time at my script, I have set it to Time.timeSinceLevelLoad And now it works

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda