Hi guys,
I have a master script that calls a function in a script attached to various instantiated objects.
The problem is that the instantiated objects don’t exist until after the game has started. They’re cloned along the way. Because they don’t exist at the beginning of the game, Unity is throwing an error.
My question is, is there a way to write a conditional statement that will only call the function IF the other script exists. Something like:
if (otherscript != null)
{
otherscript.MyFunction();
}
I’m not sure of the proper syntax or if this is even possible. As always, thanks in advance for the help.
:roll:
Well yes, that is indeed the syntax to check if something is null.
I tried this syntax but I still get the error (“an instance of type otherscript is required to access non static member MyFunction”).
I’m guessing that because the script doesn’t yet exist (although it will when the function is called) Unity gives me the error.
Thanks for the reply!
Oh… that’s because you’re trying to call the method on the type instead of an actual instance. You have to get the script instance from whatever game object it’s on and then call it.
Which means
var myScript : SomeScript = GetComponent(typeof(SomeScript));
if (myScript)
{
myScript.MyFunction();
}
Thanks guys. That did it.