Hi
I want my game to play timeline after player kill enemies. I tried
public GameObject[] targets
public PlayableDirector ending;
if (!targets)
{
ending.Play();
}
but it gives error that I can’t use “!” with GameObject[ ]. Any ideas how to do it?
use
if ( (targets == null) || (targets.length < 1)) {
... do stuff because the array is empty
}
Now please wait until I climb on this soapbox here… ahem…
“using c/c++ style “everything’s an int” to Interpret 0 (Zero) as false and everything else as true is really, really bad coding practice.”
(getting off soapbox)
So I did it and there is a problem. When enemy is dead, I’m destroying his GameObject using Destroy(gameObject);
The problem is that they’re still in the inspector as “Missing” (but there is no GameObject in the Hierarchy) so array is not empty.
You’ll have to loop through the array and see if any of them are non-null.
How to do it? I’m using arrays first time and documentation is very unclear.
I recommend running through the arrays tutorial (or any C# tutorial on arrays; they’re abundant) but for this specific question, here’s a way to do it.
bool foundTargetRemaining = false;
for (int t=0;t<targets.Length;t++) {
if (targets[t] != null) foundTargetRemaining = true;
}
if (foundTargetRemaining) {
// stuff
}
1 Like
Thanks, but I found a little problem with your script. It does //stuff at the start. I changed the statement to if (!foundTargetRemaining) and now does //stuff when enemies are dead. Thanks a lot!