Hello!
I’m using a “Gameobject Pool” I got an array of 100 gameobjects that I instantiate on start so when I click 1 of the 100 gameobjects is activated.
I have a Cannon that needs a target randomly but it needs to be active
So I made a script to seek in the array for a random gameobject and is working fine BUT it looks for deactive gameobjects too, i just want it to seek active objects and choose one!
This is my script:
GameObject[] ducks;
GameObject Target;
int index;
void SeekTarget(){
while (!Target.activeInHierarchy) {
index = Random.Range (0, (ducks.Length - 1));
Target = ducks[index];
}
}
So What I’m Trying to do here is a while loop that continues to check for a random object in the array until “Target” is active. The problem is that I get this error on this line " while (!Target.activeInHierarchy) {"
NullReferenceException: Object reference not set to an instance of an object
How do i do it? or theres another better way?