# \[C#\]Calling a function from another script

**URL:** https://discussions.unity.com/t/c-calling-a-function-from-another-script/417165
**Category:** C# & .NET
**Tags:** Scripting
**Created:** [July 2, 2010, 4:43am UTC](https://discussions.unity.com/t/c-calling-a-function-from-another-script/417165 "2010-07-02T04:43:04Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![KHopcraft](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/khopcraft/32/582170_2.png) [@KHopcraft](https://discussions.unity.com/u/KHopcraft)
#### Post date: [July 2, 2010, 4:43am UTC](https://discussions.unity.com/t/c-calling-a-function-from-another-script/417165/1 "2010-07-02T04:43:04Z")

</div>

I have the following code:

```auto
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?

---

<div class="post-metadata">

### Author: ![KHopcraft](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/khopcraft/32/582170_2.png) [@KHopcraft](https://discussions.unity.com/u/KHopcraft)
#### Post date: [July 2, 2010, 5:14am UTC](https://discussions.unity.com/t/c-calling-a-function-from-another-script/417165/2 "2010-07-02T05:14:46Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Radical](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/radical/32/362049_2.png) [@Radical](https://discussions.unity.com/u/Radical)
#### Post date: [July 2, 2010, 5:17am UTC](https://discussions.unity.com/t/c-calling-a-function-from-another-script/417165/3 "2010-07-02T05:17:23Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![KHopcraft](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/khopcraft/32/582170_2.png) [@KHopcraft](https://discussions.unity.com/u/KHopcraft)
#### Post date: [July 2, 2010, 5:25am UTC](https://discussions.unity.com/t/c-calling-a-function-from-another-script/417165/4 "2010-07-02T05:25:26Z")

</div>

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

It did not work.

---

<div class="post-metadata">

### Author: ![Radical](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/radical/32/362049_2.png) [@Radical](https://discussions.unity.com/u/Radical)
#### Post date: [July 2, 2010, 7:18am UTC](https://discussions.unity.com/t/c-calling-a-function-from-another-script/417165/5 "2010-07-02T07:18:25Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![KHopcraft](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/khopcraft/32/582170_2.png) [@KHopcraft](https://discussions.unity.com/u/KHopcraft)
#### Post date: [July 2, 2010, 4:06pm UTC](https://discussions.unity.com/t/c-calling-a-function-from-another-script/417165/6 "2010-07-02T16:06:39Z")

</div>

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. 😄

---

<div class="post-metadata">

### Author: ![Radical](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/radical/32/362049_2.png) [@Radical](https://discussions.unity.com/u/Radical)
#### Post date: [July 2, 2010, 4:08pm UTC](https://discussions.unity.com/t/c-calling-a-function-from-another-script/417165/7 "2010-07-02T16:08:18Z")

</div>

I’m glad it helped.🙂
