- Home /
Why isnt Time.time working when I switch scenes?
So I have 2 scenes, the main menu, and the game. When I press play on the engine and then play in the menu I can load into the game. The problem is that I am trying to instantiate objects depending on the time that the game started. I load into the game and the enemies are not spawning. Anything helps! Here is my instantiate script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class JumpSpawn : MonoBehaviour
{
public Transform[] Spawns;
public GameObject Enemy;
private float timetospawn = 0.5f;
void FixedUpdate()
{
if (Time.time == timetospawn)
{
spawnenemy();
timetospawn = timetospawn + 0.5f;
}
}
void spawnenemy()
{
int randomIndex = Random.Range(0, Spawns.Length);
for (int i = 0; i < Spawns.Length; i++)
{
if (randomIndex == i)
{
GameObject clone = Instantiate(Enemy, Spawns[i].position, Quaternion.identity);
}
}
}
}
Ask me if you need anymore details!
Answer by Void1ess · Jan 24 at 01:22 AM
Nvm I found a different solution by using WaitForSeconds. Here is the working script:`enter code here:`using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SlideSpawner : MonoBehaviour { public Transform[] Spawns; public GameObject Enemy; public Rigidbody PRig; private float timetospawn = 0.5f; private bool candoagain = true; void Start() { StartCoroutine(Spwn()); }
IEnumerator Spwn()
{
if (candoagain == true)
{
candoagain = false;
yield return new WaitForSeconds(timetospawn);
spawnenemy();
candoagain = true;
}
}
void spawnenemy()
{
int randomIndex = Random.Range(0, Spawns.Length);
for (int i = 0; i < Spawns.Length; i++)
{
if (randomIndex == i)
{
GameObject Clone = Instantiate(Enemy, Spawns[i].position, Quaternion.identity);
}
}
}
}`
Your answer
Follow this Question
Related Questions
Calling a variable 2 Answers
Instantiating GO infront of player 2 Answers
C# 2d Instantiate object randomly around gameobject 1 Answer
GameObject reference breaks 1 Answer
UnityEngine.Object.Instantiate 1 Answer