My Enemy Spawner spawns twice at once
So, let me introduce you to the situation first. I have a basic scene set up, with an enemy spawn manager:
This GameObject is responsible for spawning waves of enemies into my scene. The Enemy Prefabs are the types of enemies that can be chosen to construct a wave. I have no problem with how that works yet. The bottom part where it says Starship1 and Starship2 corresponds to two other Game Objects that I have in the scene:
These just rotate around the Planet using the waypoints there, and shoot bullets. This part is fine as well. But, here is the Update() function from Spawn Management script:
void Update()
{
if (finished_maneuvering)
{
if (game_timer > game_timer_this_diff)
{
DifficultyUp();
}
game_timer += Time.deltaTime;
if (enemy_this_wave == 0)
{
bool r = Random.Range(0, 100) < 70;
if (false)
{
SpawnNextWave();
}
else
{
CallStarShip();
}
}
}
}
Ignore the game timer and difficulty stuff. For testing purposes of the starships, I have made it so that, anytime this script attempts to spawn a wave, it's a starship call. Which does this:
void CallStarShip()
{
bool r = Random.Range(0, 100) > 50;
if (r)
{
Starship1.GetComponent<StarShipScript>().InitializeStarship();
}
else
{
Starship2.GetComponent<StarShipScript>().InitializeStarship();
}
}
This is supposed to choose either one of the starships, and call the method InitializeStarship() from its script (which I've surely attached to both). This is what InitializeStarship() looks like:
public void InitializeStarship()
{
faded_in = false;
moved = false;
faded_out = false;
shoot = true;
waitbeforefire /= Mathf.Clamp(SpawnManagement.difficulty_level, 1, 2.3f);
}
Don't worry about difficulty level, it's not the source of error, that is a static variable. These values just play the role of telling the starship to start moving between waypoints, which may not be the most efficient way of doing it, but that's how I could do it. Anyway, after the Starship has finished it's route, it is supposed to go back to a place I called waypoint_4 and in the code it looks like this:
. . code in here . .
else
{
transform.position = Vector3.Lerp(transform.position, waypoint_4.position, fade_speed * Time.deltaTime);
if (spawner != null)
{
spawner.finished_maneuvering = true;
}
}
I'm pretty sure that spawner isn't null anyway, because it was set in the Start() function like this:
void Start()
{
spawner = GameObject.FindWithTag("enemyspawner").GetComponent<SpawnManagement>();
transform.position = waypoint_4.position;
}
Alright now let's get to what the problem is: When I start the game, I expect the SpawnManager to initialize either the Top starship, or the Bottom one. However both of them get initialized at once, and even more than once (I checked by adding Debug.Log() statements before the function call CallStarship() to test it). And, after the starships go away, new starship call should be made according to my code, but nothing is done. Why could this be happening? This is my first ever serious project, so excuse me if this is something simple, but I searched online, looked at places, and just couldn't find a solution to this. Any help would be appreciated.
Your answer
Follow this Question
Related Questions
Change color of object with C# 1 Answer
Gameobject array 0 Answers
IndexOutOfRangeException error when getting transform of array object 0 Answers
Spawn in a different and random Spawn point from a list 0 Answers
Instantiate prefab with its component c# 0 Answers