# Script can't call another script's function

**URL:** https://discussions.unity.com/t/script-cant-call-another-scripts-function/538812
**Category:** Scripting
**Tags:** Scripting
**Created:** [May 25, 2014, 5:12pm UTC](https://discussions.unity.com/t/script-cant-call-another-scripts-function/538812 "2014-05-25T17:12:33Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Nitride](https://avatars.discourse-cdn.com/v4/letter/n/a4c791/32.png) [@Nitride](https://discussions.unity.com/u/Nitride)
#### Post date: [May 25, 2014, 5:12pm UTC](https://discussions.unity.com/t/script-cant-call-another-scripts-function/538812/1 "2014-05-25T17:12:33Z")

</div>

Hi, I’d appreciate some help. I’m trying to call a function but it won’t let me because it can’t find it.

This is where the functions are defined:

```auto
    public void ThrustUp()
    {
        m_parentDriller.rigidbody2D.AddForceAtPosition(thrust, transform.position);
	    CreateThrust(transform.position + transform.up*-0.5f, transform.up*-4.0f, 1.0f);
        m_diller.CurrentHeat += thrustHeatAmount;
    }

    public void ThrustDown()
    {
        m_parentDriller.rigidbody2D.AddForceAtPosition(-thrust, transform.position);
        CreateThrust(transform.position + transform.up * 0.2f + transform.right * 0.4f, transform.up * 3.0f, 0.5f);
        CreateThrust(transform.position + transform.up * 0.2f - transform.right * 0.4f, transform.up * 3.0f, 0.5f);
        m_diller.CurrentHeat += thrustHeatAmount;
    }

```

This is where they are called:

```auto
if (transformCache.position.y > 1)
        {
            driller.ThrustUp();
        }
        if (transformCache.position.y < 1)
        {
            driller.ThrustDown();
        }

```

This is also in the script above:

```auto
public GameObject driller = GameObject.Find("DrillerEngine");

```

---

<div class="post-metadata">

### Author: ![AlpacaMaster](https://avatars.discourse-cdn.com/v4/letter/a/e8c25b/32.png) [@AlpacaMaster](https://discussions.unity.com/u/AlpacaMaster)
#### Post date: [May 25, 2014, 5:19pm UTC](https://discussions.unity.com/t/script-cant-call-another-scripts-function/538812/2 "2014-05-25T17:19:44Z")

</div>

instead of making the driller variable a GameObject, make it the type driller( assuming that is the name of the script you are trying to use it’s function)  
and then make it be the component of the script in the object, so:

```auto
public driller driller = GameObject.Find("DrillerEngine").GetComponent("driller");

```

---

<div class="post-metadata">

### Author: ![Lanre](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@Lanre](https://discussions.unity.com/u/Lanre)
#### Post date: [May 26, 2014, 2:19am UTC](https://discussions.unity.com/t/script-cant-call-another-scripts-function/538812/3 "2014-05-26T02:19:01Z")

</div>

Hello there. Try these steps:

1. Check if the compiler actually finds your driller gameobject. I typically advise you to use GameObject.FindGameObjectWithTag(“”); instead of Find because tags are more precise (less room for spelling errors). And if you want to have best performance and least issues, just define the driller gameObject in your Awake() and assign the gameobject in editor.

2. call your function by using “as” and whaterver. By this i mean:

```auto
public GameObject DrillerGameObject;
 //assign this is editor
public driller Driller;

void Update() {
if (whateverconditionyouwant) {
Driller = (DrillerGameObject.GetComponent("driller") as driller;
Driller.whateverfunctionyouwannacall();
}
}

```

That should work perfectly

---

<div class="post-metadata">

### Author: ![Nitride](https://avatars.discourse-cdn.com/v4/letter/n/a4c791/32.png) [@Nitride](https://discussions.unity.com/u/Nitride)
#### Post date: [May 26, 2014, 9:19pm UTC](https://discussions.unity.com/t/script-cant-call-another-scripts-function/538812/4 "2014-05-26T21:19:29Z")

</div>

Thanks for helping me solve this issue guys.
