[C#]Calling a function from another script

I have the following code:

BehindScenes otherScript = behindScenes.GetComponent<BehindScenes>();
otherScript.DeadTank();

All the function of otherScript does is make a boolean false.

The function is not called, any idea why?

Oops, by the way, there is no error or anything. It just doesn’t work.

I’m assuming behindScenes is a GameObject?

Try this instead:

BehindScenes otherScript = behindScenes.GetComponent(typeof(BehindScenes)) as BehindScenes;
otherScript.DeadTank();

Let me know how it works out.

you are indeed quite correct to assume that it is a GameObject :).

It did not work.

If behindScenes GameObject isn’t null, and the BehindScenes script is attached to it, otherScript.DeadTank() should be being called.

Please double check that either: 1) behindScenes is public, and the correct GameObject is assigned to it in the editor, or 2) that you’re finding and assigning the GameObject to the behindScenes variable in code.

One more thing. If the GameObject containing the BehindScenes script is disabled, GameObject.Find(“GAMEOBJECTNAME”) will fail.

So instead of predefining the gameobject, which was not null, I made it search for a gameobject with a tag and it worked properly. Thanks for your help. :smile:

I’m glad it helped.:slight_smile: