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;
}

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.

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.");