Instantiating more enemies based on player score.

I don’t know what exactly I’ve done wrong here, but the enemy wont instantiate… What I want is for a new instance of the enemy to instantiate once the player has reached a certain distance, or score. Here’s my code:

		if(playerDistance == (int)10)
		{
			GameObject GO = Instantiate(enemy) as GameObject;
			
			GO.transform.parent = ParentGameObject.transform;
		}

enemy is defined in the Awake() function with GameObject.Find(“Enemy”)… I don’t know what could be wrong here other than maybe the player never actually reaches a whole number of 10… The distance is set to a float variable.

Hi

I’m not sure , but try

if ( (int) PlayerDistance == 10 )

You try to compare float to int .

if you think the position is never over 10 , trace the value in the console and adapt the value . You might try to do

if ( (int) PlayerDistance > 10 )

if the player is far you spawn an enemy .

Good luck

Lol I was type casting the wrong thing xP

I got it to work, but now it’s instantiating the enemy every frame that the playerDistance is a 10…

I don’t know how to fix this… I know there has got to be a better way to do it than an if statement.

As long as the condition is true, assuming your code lives within the update function or something similar, it will trigger every update. To get around this, you can use a second variable to check whether the condition has already triggered. For instance, if you only want the enemy to spawn once, you can add this outside of the function:

var HasSpawned : boolean = false;

Then, add this variable as a condition, like this:

if ( (int) PlayerDistance > 10  !HasSpawned )
{

    GameObject GO = Instantiate(enemy) as GameObject;
    GO.transform.parent = ParentGameObject.transform;
    HasSpawned = true;

}

Hope this helps.

Please tell me what special the (int) is for? I never used it?

Enough? Isn’t it? Because of java?

(int) is a type cast. Since the playerDistance variable is originally a float, I am type casting it as an int, therefore while it is being called locally within the if statement, it is an int. But globally, within the PlayerMove class, it is still a float.

@chjacobsen, you did it man! Well, for the most part… But when I try to replicate it, it just instantiates the enemy like a million times… I don’t get it… Here’s my code

if((int)playerDistance == 10.0f  !HasSpawned)
			{
           GameObject GO = Instantiate(enemy) as GameObject;
			
           GO.transform.parent = ParentGameObject.transform;
			HasSpawned = true;
			
			}
		else if ((int)playerDistance > 10.0f  HasSpawned)
		{
		HasSpawned = false;
		}
		
		if((int)playerDistance == 20.0f  !HasSpawned)
			{
           GameObject GO = Instantiate(enemy) as GameObject;
			
           GO.transform.parent = ParentGameObject.transform;
			HasSpawned = true;
			}
		else if ((int)playerDistance > 20.0f  HasSpawned)
		{
		HasSpawned = false;
		}

it works just fine without the second If/else if statement… I don’t know whats happening.

Learned something new, ty…

No problem (:

Okay, I fixed the issue with the second instantiation… I just don’t know how to set HasSpawned back to false…xP.

So, my second instantiation won’t instantiate!

more code below xP

		if((int)playerDistance == 10.0f  !HasSpawned)
			{
           GameObject GO = Instantiate(enemy) as GameObject;
			
           GO.transform.parent = ParentGameObject.transform;
			HasSpawned = true;
			}
		
			
		
		if((int)playerDistance == 20.0f  !HasSpawned)
			{
           GameObject GO1 = Instantiate(enemy) as GameObject;
			
           GO1.transform.parent = ParentGameObject.transform;
			HasSpawned = true;
			}

There is a problem with your code , you never set HasSpawned to false , so it Will never work .

This is not a good solution but If you want something easy.

I don’t have time right now , I’ll come with proper solution soon

I figured it out. I just have to make a new bool for every if statement. Thanks for all the help guys!