- Home /
Question by
KlashnikovKid · Sep 13, 2012 at 05:34 PM ·
c#null reference exceptiongetcomponents
C# Null Reference exception with GetComponents()
Hey, new to Unity!
So I'm trying to grab all the components of a specific type from a GameObject and I get a null reference exception due to the FireScript array not getting assigned an instance. I know these components exist since the spawner is instantiating enemies with the correct behavior. (I'm trying to get them to fire in sync at the moment)
So what could be going on here?
FireScript[] _fireScripts = _enemy.GetComponents(typeof(FireScript)) as FireScript[];
for (int i = 0; i < _fireScripts.GetLength(0); ++i) // Null reference exception upon calling for the array's length.
_fireScripts[i].fireTimer += elapsedActiveTime;
Comment
Best Answer
Answer by DaveA · Sep 13, 2012 at 05:58 PM
Try this:
FireScript[] _fireScripts = _enemy.GetComponents<FireScript>();
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Showing unity ads from other script? 1 Answer
Setting an object from a pool active not working 1 Answer
Null reference exception 1 Answer