- Home /
Object reference not set to instance of an object - when it is!
I've had this error many times, and always when the object can't be found. But this time the object IS found, and the script thrown an error. But it's not a fatal error and the game plays just as it should. The same script actually makes use of the object which it's complaining about, and it's fine. I can't see a problem, or why I'm getting this error.
Here's my code. I'm getting a child object of the object the script is attached to (there are others in the game with the same name), hence why I use transform.Find() and not GameObject.Find()
private var fireFrom: GameObject;
function Start() {
fireFrom = transform.Find("FireFrom").gameObject;
}
show us your hierarchy and where the script is assigned
Normally I would do that @RLin, but in this case I need to access a child object. I use transform.Find("FireFrom").gameObject elsewhere to find a child object, and it doesn't thrown an error. Is there a better way to find a child object maybe?
@YoungDeveloper I have "Turret", inside that is "TurretHead" (where this script is), and inside that is "FireFrom".
Answer by Zambii · Jul 08, 2015 at 09:10 AM
I think the problem is that unity does not know specifically which one you want to use the transform since you are telling unity to find any transform that is attached to a game object called FireFrom and since there are more then one in the scene with the same name, unity gets confused. I don't know what you are trying to do with that transform, but it seems to me like that is where you want to instantiate a projectile from, so one simple way to fix it would be to make the transform be public and set the transform directly from within unity. In C# it would be something like this:
public Transform fireFrom;
//And that is all since you will be manually selecting the transform from within unity.
Answer by ransomink · Jul 08, 2015 at 09:56 PM
Make sure "FireFrom" is spelled the same inside the hierarchy and the script. Also check that the script is attached to the TurretHead gameobject (just as a formality). I would throw an debug exception to see if the FireFrom gameobject comes back as null.
fireFrom = transform.Find("FireFrom").gameObject;
if (fireFrom == null)
Debug.LogError("No child with the name 'FireFrom' attached to TurretHead.");
Errr, make sure that 'FireForm' is spelled the same as 'FireFrom'? ;)
But yes, I'd do both of those things too.
Other things that might be worth looking for...
The object being destroyed after the reference is cached.
There being multiple GameObjects called 'FireFrom'.