Null Reference

Hello, for my Tower prefab. Basically what I did was have a script called tower at the top level of the prefab, then have a bullet script attached to the bullet prefab for the particle system of the bullet fx. Then I set the rate of my bullet shoots from the tower script at top levelof the prefab by using getcomponentchildren() to access the bullet script attached to the bullet prefab… However when I instantiate in game, I keep getting this null reference error regarding the setbulletspeed() function. Although everything works, I’d still like to know why it’s having this error

Thank!

Hi William,

Are there any children parented to the same game object to which you attached the Tower component? They must be there when Start is being executed. Otherwise, the method returns null.


See also;

Hi Nina,
For sure there is the gameobject attached .The tower component has two children. Tower base and Tower top. and under Tower top there’s a gameobject called bullet. What I did in the tower script was getcomponentchildren() to search the gameobject bullet and get its particlesystem component. Every thing works in play mode. But this null reference keeps showing up with out any error i can see.

Thanks,
Will

Check if the script is attached to another game object in your scene.

This topic was automatically closed after 14 days. New replies are no longer allowed.

Privacy & Terms