Accessing parent gameobject script not working

Any ideas why the console keeps returning null? The parent gameobject has a script attached named “AIFollow”.

private var AIFollowScript : AIFollow;

function Start () {
	AIFollowScript = transform.parent.gameObject.GetComponent(AIFollow);
	Debug.Log(AIFollowScript);
}

It’s definitely not parent being null. If that were the case, the code would throw a NullReferenceException because you’d be trying to call GetComponent on something that was null, and that isn’t the case.

This might be a shot in the dark too, but I think this is a script language thing. From the screenshot provided, it follows that AIFollow is a C# script, but the script you’re calling it from is clearly JavaScript.

Please see this:

http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponent.html

And note, specifically, the sentence:

“It is better to use GetComponent with a Type instead of a string for performance reasons. Sometimes you might not be able to get to the type however, for example when trying to access a C# script from Javascript. In that case you can simply access the component by name instead of type.”

This is exactly your situation. It’s probably failing due to the type inspecificity of javascript versus the type safety of C#, and you’re using the generic version of GetComponent in your code.

Try switching to the string version and fetch it using,

transform.parent.gameObject.GetComponent("AIFollow"); 

instead.