assigning game object variable at runtime + "for" problem

Hello creators!

I’m trying to build a space base protection that works automoatically. When an enemi is detected, 2 ships are selected to go and protect the base. I don’t want that is allways the same ships that go, i want to make a turn by turn (they are 16 ships, if the first and second one are going to fight and comming back, on the next alert, the ship 3 4 will be sellected , then 4, 5 and etc…) When all the 16 ships was going to fight, the first one will be sellected again, and it should loop like this.

For that, i was creating an empty game object that represent a disponibility switch (i was going to fight this turn, i’m waiting the next / i’m ready to fight, it’s my turn) When this game object is active, the ship can be sellected if an alarm occurs, if he is not active, he is “busy” and can not be sellected until the next turn.

So in the theory, should work… but my script seems to have some errors… if someone can help so solve it.

(errors : the empty that represent the disponibility of the ship should turn inactive if he is choosen, but he don’t turn inactive ; When the ships go, they need to have a fixed position in the trooper for play a specific animation, so in my script i put the first ship sellected in the first position of the trooper, the second sellected in the second place etc… i can not assign the sellected game object to another variable that represent the same game object…)

The script:

var dispo = new GameObject[16]; // the game object that define if the ship is disponible or not
var ship = new GameObject[16]; // the ship himself, for play the animation
private var elu = new GameObject[1]; // this is the variable that supose to take the valor of the sellected game object for the position in the trooper

var i = 0;
var j = 0;

function Start ()
{
	yield WaitForSeconds(2);
	Alert2();
	yield WaitForSeconds(5);
	Alert2();
	yield WaitForSeconds(5);
	Alert2();
}

function Alert2()
{
	for (j = 0; j == 2; j++) // the condition loop until we find 2 ships availlables.
	{
		for (i = 0; i == 16; i++) // the condition loop until we check all the ships. this condition stop if we have find the 2 ships.
		{
			if (dispo[i].active == true) // if the disponibility game object is active
			{
				dispo[i].active = false; // we put him inactive > Here is the first bug i meet, he don't turn inactive (???)
				elu[j] = ship[i]; // i think that here is a problem, he don't want to set elu1 as the ship[i] sellected game object
				j++;
				i++;
			}
			else
			{
				i++;
			}
		}
	}
	Intervention2(); // we supose to find the 2 ships, now we will ask them to play animations etc...
}

function Intervention2()
{
	Debug.Log("The ship "+(i+1) +" was sellected for go in the place "+(j+1) +" of the intervention tropper"); // this works if i don't ask to disable elu1 (next line)
	elu[0].active = false; // before playing an animation i just try to desactivate him, but i have an error (look down in my post)
}

so this is the error message :
NullReferenceException: Object reference not set to an instance of an object
ArraySupr.Intervention2 () (at Assets\ArraySupr.js:43)
ArraySupr.Alert2 () (at Assets\ArraySupr.js:37)
ArraySupr+Start$3+$.MoveNext () (at Assets\ArraySupr.js:11)

Hey

Didn’t test your code but it seems your ‘for’ conditions are off by 1. You always start at 0 but test == 2 and == 16 which means that there will be 3 iteration in the outer loop and 17 iterations in inner loop.

Perhaps that’s where the null reference is coming from.

Hope this helps.

uuh, is not suposed to go out of the “for” when the number is on? For me the variable j (for example) enter at 0, check, if it’s ok, the variable is 1, he check again, if is ok, the variable is 2 so he go out… 2 iterations, isn’t it?

Hey,

No the j variable will start at 0 in the first loop. Then it will become 1 in the second loop…so at this point you already had 2 iterations. On the third iteration j will become 3 and that’s where you get in trouble.

Usually loop conditions are written like so:

for (j = 0; j == count - 1; j++) {}

since in your case count is predetermined you might as well write

for (j = 0; j == 1; j++) {}

and

for (i = 0; i == 15; i++) {}

Ok i understand thank you, i changed it… didn’t resolve my 2 problems (but thank you anyway because this resole a problem that would appear after).

So for more precision, the 2 problems that stays:

  • The game object that need to be disactivated after the check is positive… don’t disactivate himself ( dispo*.active = false; ).*
    - Seems that like i wrote it, unity don’t want to convert “elu” in game object taking the variable of the choosen one. ( elu[j] = ship*; )*
    here is the new script with the correction of alexfeauture :slight_smile:
    ```
    *var dispo = new GameObject[9]; // the game object that define if the ship is disponible or not
    var ship = new GameObject[9]; // the ship himself, for play the animation
    private var elu1 = new GameObject[2]; // this is the variable that supose to take the valor of the sellected game object for the position in the trooper

var i = 0;
var j = 0;

function Start ()
{
yield WaitForSeconds(2);
Alert2();
yield WaitForSeconds(5);
Alert2();
yield WaitForSeconds(5);
Alert2();
}

function Alert2()
{
for (j = 0; j == 1; j++) // the condition loop until we find 2 ships availlables.
{
for (i = 0; i == 15; i++) // the condition loop until we check all the ships. this condition stop if we have find the 2 ships.
{
if (dispo[i].active == true) // if the disponibility game object is active
{
dispo[i].active = false; // we put him inactive > Here is the first bug i meet, he don’t turn inactive (???)
elu1[j] = ship[i]; // i think that here is a problem, he don’t want to set elu1 as the ship[i] sellected game object
}
}
}
Intervention2(); // we supose to find the 2 ships, now we will ask them to play animations etc…
}

function Intervention2()
{
Debug.Log(“The ship “+(i+1) +” was sellected for go in the place “+(j+1) +” of the intervention tropper”); // this works if i don’t ask to disable elu1 (next line)
elu1[0].animation.Play(“Move”); // before playing an animation i just try to desactivate him, but i have an error (look down in my post)
}
_
```*_