- Home /
Problem with foreach loop with GameObject array
I have a few enemies in my scene that I want to be active one at a time. As i destroy an enemy, the next one will become active. Here's what I'm working with:
EnemyAI.cs
public static bool isActive = true;
void Update()
{
if(isActive == false)
{
GetComponent<EnemyAI>().enabled = false;
}
}
In the script above, I have the isActive bool set to true but I want the script to be disabled if it is set to false, here's where my problem is:
EnemyBehaviour.cs
public int shipCount = 13;
void Start()
{
GameObject[] ships = new GameObject[shipCount];
for(int i = 1; i < shipCount; i++)
{
ships[i] = GameObject.FindWithTag ("ship" + i);
foreach(GameObject ship in ships[i])
{
EnemyAI.isActive = false;
}
}
}
I want to find all of my ships with the correct tags and set them to false (except for the first one, that's why I start the loop at 1)
I used a for loop to find the objects and I wanted to then use a foreach loop to iterate through that list and set each ships (not including the first one) bool isActive to false but it throws me this error:
I'm a bit stumped as to how to work around it so if anyone could help me out, I'd appreciate it
Answer by ajketan · Aug 23, 2014 at 11:35 AM
public int shipCount = 13;
void Start()
{
GameObject[] ships = new GameObject[shipCount];
for(int i = 1; i < shipCount; i++)
{
ships[i] = GameObject.FindWithTag ("ship" + i);
}
foreach(GameObject ship in ships)
{
EnemyAI.isActive = false;
}
}
I think this should work for you.... because for a foreach loop you need a collection of objects...in your case an array 'ships' of GameObject type.
Plus ... I am not aware of your game logic... if you want 13 ships then use
for(int i = 1; i < shipCount + 1; i++)
//instead of
for(int i = 1; i < shipCount; i++)
because according to your logic the for loop will execute only 12 times...
Hope that helped...
And thanks...for the vote up in advance... :P
You misunderstood my question so I'm afraid you don't get an upvote ;)
I want my loop to execute 12 times as I don't want my first ship to be disabled, I only want ships with the index 1 through 12 to be disabled, leaving ship 0 active.
The code above disables all of my ships.
my actual reply is for the foreach loop that you put inside the for loop... it should be out of the for loop... then only it will work... and don't bother the vote up for this ;P
The answer that @Bored$$anonymous$$ormon gave is same as $$anonymous$$e and which is correct...
Cheers...!
Answer by Kiwasi · Aug 23, 2014 at 11:26 AM
This line
foreach(GameObject ship in ships[i])
Should be this
foreach(GameObject ship in ships)
That returns the following:
The type or namespace name `ships' could not be found. Are you missing a using directive or an assembly reference?
Whoops, my bad. $$anonymous$$ill the [] as well. Guess that's a sign that its to late for me to be answering any more questions.
Have edited my answer to be correct now.
You probably also want to consider this line inside the loop. As it stands your loop does nothing to each individual ship.
ship.GetComponent<EnemyAI>().isActive = false;
Cheers @Bored$$anonymous$$ormon, last question for ya. I'm now getting:
Static member `EnemyAI.isActive' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d
You really don't want isActive to be a static member. Go back to your EnemyAI script and remove the static modifier from the declaration of isActive.
Any variable you intend to use for each ship can't be static.