- Home /
GetComponent(NameOfScript) error "reference not set to an instance"
Hi,
I am trying to disable one script from another, but I got errors only. Any help please?
function Start () {
GetComponent(NameOfMyScript).enabled=false;
}
And the error is this, so I guess that some reference is missing, but I don't know how to fix it.
"NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, System.Object[] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Object[] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.SetProperty (System.Object target, System.String name, System.Object value) ScriptHandler.Start () (at Assets/Scripts/ScriptHandler.js:4)"
I assume that "NameOf$$anonymous$$yScript" is really the name of the script you're trying to disable?
Yes, and ScriptHandler is the one from I am trying to disable
Answer by DaveA · Apr 05, 2011 at 08:15 PM
The game object that this script is on does not have a NameOfMyScript component attached to it is my guess. Try this:
var ms : NameOfMyScript = GetComponent(NameOfMyScript);
if (ms)
ms.enabled = false;
else
Debug.LogError("There is no NameOfMyScript component");
It seems it is not working. Thank you anyway, I am working based on your answer. i will post here if i found the solution
yeah you were right, my issue was that the script was in another object so I had to Find() that object first... Thanks DaveA