# NullReferenceException when using Invoke

**URL:** <https://discussions.unity.com/t/nullreferenceexception-when-using-invoke/598853>\
**Category:** Scripting\
**Tags:** Scripting\
**Created:** [September 24, 2015, 12:22pm UTC](https://discussions.unity.com/t/nullreferenceexception-when-using-invoke/598853 "2015-09-24T12:22:37Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![MazeTard](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@MazeTard](https://discussions.unity.com/u/MazeTard)\
**Post date:** [September 24, 2015, 12:22pm UTC](https://discussions.unity.com/t/nullreferenceexception-when-using-invoke/598853/1 "2015-09-24T12:22:37Z")

</div>

Hello!  
I’m having trouble with an Invoke call. I’m constructing a tower and i need the construction to finish after a calculation of build time. I have tested through all my variables in the calculations and nothing is null. However, when i call my Invoke(string name, float time), i get the NullReferenceException. He’s the code:

```auto
public void PrepareConstruction(){
        selection = (GameObject)Instantiate(constructionGo, towerBase.transform.position, Quaternion.identity);
        currentTower.SetActive (false);
        constructionGo.SetActive (true);
        CalculateBuildTime (towerScript.towerUpgrade [index].options [0].buildCrew);
    }

    void FinishConstruction(){
        constructionGo.SetActive (false);
        currentTower.SetActive (true);
    }
      
    public void CalculateBuildTime(int buildCrew){
        int pointsPerSec = buildCrew * GameStats.instance.workerPointValue;
        int seconds = towerScript.towerUpgrade [index].options [0].buildPoints /pointsPerSec ;
        Invoke ("FinishConstruction", seconds);
    }

```

Please help me!

---

<div class="post-metadata">

**Author:** ![martinmr](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@martinmr](https://discussions.unity.com/u/martinmr)\
**Post date:** [September 24, 2015, 12:48pm UTC](https://discussions.unity.com/t/nullreferenceexception-when-using-invoke/598853/2 "2015-09-24T12:48:16Z")

</div>

can you tell at which point exactly the nullref appears ?

---

<div class="post-metadata">

**Author:** ![karl\_jones](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/karl_jones/32/83270_2.png) [@karl\_jones](https://discussions.unity.com/u/karl_jones)\
**Post date:** [September 24, 2015, 12:49pm UTC](https://discussions.unity.com/t/nullreferenceexception-when-using-invoke/598853/3 "2015-09-24T12:49:37Z")

</div>

Can you post the full error and also use [code tags](http://forum.unity3d.com/threads/using-code-tags-properly.143875/)?  
My guess would be that constructionGo or currentTower is null.

---

<div class="post-metadata">

**Author:** ![MazeTard](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@MazeTard](https://discussions.unity.com/u/MazeTard)\
**Post date:** [September 24, 2015, 12:57pm UTC](https://discussions.unity.com/t/nullreferenceexception-when-using-invoke/598853/4 "2015-09-24T12:57:40Z")

</div>

Thanks for quick responses 🙂  
Error comes when it reaches Invoke (“FinishConstruction”, seconds);  
Neither constructionGo nor currentTower is null at that time.

NullReferenceException: Object reference not set to an instance of an object  
Construction.PrepareConstruction () (at Assets/scripts/Construction.cs:25)  
GUILogic.BuyTower () (at Assets/imported/3D Tower Defense Starter Kit/Scripts/GUI/GUILogic.cs:540)  
GUIImpl.BuyTower () (at Assets/imported/3D Tower Defense Starter Kit/Scripts/GUI/GUIImpl.cs:435)  
GUIImpl.ProcessGrid () (at Assets/imported/3D Tower Defense Starter Kit/Scripts/GUI/GUIImpl.cs:239)  
GUIImpl.Update () (at Assets/imported/3D Tower Defense Starter Kit/Scripts/GUI/GUIImpl.cs:167)

---

<div class="post-metadata">

**Author:** ![MazeTard](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@MazeTard](https://discussions.unity.com/u/MazeTard)\
**Post date:** [September 24, 2015, 12:59pm UTC](https://discussions.unity.com/t/nullreferenceexception-when-using-invoke/598853/5 "2015-09-24T12:59:22Z")

</div>

Error comes in line 17.  
Neither of those objects are null at this time.

This is the error message:  
NullReferenceException: Object reference not set to an instance of an object  
Construction.PrepareConstruction () (at Assets/scripts/Construction.cs:25)  
GUILogic.BuyTower () (at Assets/imported/3D Tower Defense Starter Kit/Scripts/GUI/GUILogic.cs:540)  
GUIImpl.BuyTower () (at Assets/imported/3D Tower Defense Starter Kit/Scripts/GUI/GUIImpl.cs:435)  
GUIImpl.ProcessGrid () (at Assets/imported/3D Tower Defense Starter Kit/Scripts/GUI/GUIImpl.cs:239)  
GUIImpl.Update () (at Assets/imported/3D Tower Defense Starter Kit/Scripts/GUI/GUIImpl.cs:167)

---

<div class="post-metadata">

**Author:** ![MazeTard](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@MazeTard](https://discussions.unity.com/u/MazeTard)\
**Post date:** [September 24, 2015, 1:14pm UTC](https://discussions.unity.com/t/nullreferenceexception-when-using-invoke/598853/6 "2015-09-24T13:14:07Z")

</div>

Invoke has two parameters, the string name and float time…  
The time is seconds, which is not null, and the string name is the name of the method just above it.  
Nothing is null.  
Yet the error comes with the invoke.

---

<div class="post-metadata">

**Author:** ![KelsoMRK](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kelsomrk/32/205687_2.png) [@KelsoMRK](https://discussions.unity.com/u/KelsoMRK)\
**Post date:** [September 24, 2015, 1:39pm UTC](https://discussions.unity.com/t/nullreferenceexception-when-using-invoke/598853/7 "2015-09-24T13:39:17Z")

</div>

The error is telling you that it’s on line 25, not 17. It’s also in the PrepareConstruction method which does not contain an Invoke. Therefore, something in that method is nulll; could be selection, or constructionGo, or towerBase, or currentTower, or towerScript, or towerUpgrade at index, or options at 0

---

<div class="post-metadata">

**Author:** ![martinmr](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@martinmr](https://discussions.unity.com/u/martinmr)\
**Post date:** [September 25, 2015, 5:40am UTC](https://discussions.unity.com/t/nullreferenceexception-when-using-invoke/598853/8 "2015-09-25T05:40:15Z")

</div>

Line 25 in his code … i don’t think he postet his hole script 😉

---

<div class="post-metadata">

**Author:** ![MazeTard](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@MazeTard](https://discussions.unity.com/u/MazeTard)\
**Post date:** [September 25, 2015, 7:43am UTC](https://discussions.unity.com/t/nullreferenceexception-when-using-invoke/598853/9 "2015-09-25T07:43:14Z")

</div>

Yeah the line numbers does not match because i didnt post it all.  
Dont mind that.  
The error is in the invoke call, neither of the values are null.  
@KelsoMRK none of those values are null.

---

<div class="post-metadata">

**Author:** ![MazeTard](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@MazeTard](https://discussions.unity.com/u/MazeTard)\
**Post date:** [September 25, 2015, 7:56am UTC](https://discussions.unity.com/t/nullreferenceexception-when-using-invoke/598853/10 "2015-09-25T07:56:29Z")

</div>

```auto
public class Construction : MonoBehaviour {

    public GameObject constructionGo;
    public GameObject selection { get; set;}
    public TowerManager towerScript{ get; set;}
    public int index{ get; set;}
    public TowerBase towerBase{ get; set;}
    public GameObject currentTower { get; set;}

    // Use this for initialization
    void Start () {

    }
  
    // Update is called once per frame
    void Update () {
  
    }

    public void PrepareConstruction(){
        selection = (GameObject)Instantiate(constructionGo, towerBase.transform.position, Quaternion.identity);
        currentTower.SetActive (false);
        constructionGo.SetActive (true);
        float seconds = CalculateBuildTime (towerScript.towerUpgrade [index].options [0].buildCrew);
        if (constructionGo != null && selection != null && towerScript != null && index != null && towerBase != null && currentTower != null && seconds != null) {
            print ("Nothing is null!");
            Invoke ("FinishConstruction", seconds);
        }
    }

    void FinishConstruction(){;
        constructionGo.SetActive (false);
        currentTower.SetActive (true);
    }
      
    public float CalculateBuildTime(int buildCrew){
        int pointsPerSec = buildCrew * GameStats.instance.workerPointValue;
        float seconds = towerScript.towerUpgrade [index].options [0].buildPoints /pointsPerSec ;
        print (seconds);
        print (constructionGo);
        print (currentTower);
        return seconds;
    }
}

```

Please! Now help me! I already told you nothing is null!  
I tried switching it up abit, calling the invoke elsewhere, but it¨s still the exact same error… It seems like the Invoke method itself is broken.

---

<div class="post-metadata">

**Author:** ![KelsoMRK](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kelsomrk/32/205687_2.png) [@KelsoMRK](https://discussions.unity.com/u/KelsoMRK)\
**Post date:** [September 25, 2015, 12:51pm UTC](https://discussions.unity.com/t/nullreferenceexception-when-using-invoke/598853/11 "2015-09-25T12:51:59Z")

</div>

Yes it is. Unity isn’t lying to you. Post the full script and the error so the line numbers match. Or alternately post the full error and the line of code the error is reference.

---

<div class="post-metadata">

**Author:** ![Suddoha](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/suddoha/32/110358_2.png) [@Suddoha](https://discussions.unity.com/u/Suddoha)\
**Post date:** [September 25, 2015, 1:27pm UTC](https://discussions.unity.com/t/nullreferenceexception-when-using-invoke/598853/12 "2015-09-25T13:27:19Z")

</div>

> [@MazeTard](#):
>
> Please! Now help me! I already told you nothing is null!  
> I tried switching it up abit, calling the invoke elsewhere, but it¨s still the exact same error… It seems like the Invoke method itself is broken.

What’s more likely: you’ve made a mistake or the invoke method is “broken”?

You still haven’t checked everything that @KelsoMRK mentioned.

(Assuming that the type of the elements in the towerUpgrade and options arrays are reference types.)

- towerScript.towerUpgrade (the array itself)
- towerScript.towerUpgrade[index] (the element you’re trying to access)
- towerScript.towerUpgrade[index].options (the options array)
- towerScript.towerUpgrade[index].options[0] (the element in the array)

I’d recommend to break that down a little. Too much chaining.

---

<div class="post-metadata">

**Author:** ![karl\_jones](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/karl_jones/32/83270_2.png) [@karl\_jones](https://discussions.unity.com/u/karl_jones)\
**Post date:** [September 25, 2015, 6:43pm UTC](https://discussions.unity.com/t/nullreferenceexception-when-using-invoke/598853/13 "2015-09-25T18:43:40Z")

</div>

If the invoke function was broke we would know about it, lots of people use it  
Put some asserts in to check all of your references.

---

<div class="post-metadata">

**Author:** ![FogCZ](https://avatars.discourse-cdn.com/v4/letter/f/ecccb3/32.png) [@FogCZ](https://discussions.unity.com/u/FogCZ)\
**Post date:** [October 10, 2020, 1:20pm UTC](https://discussions.unity.com/t/nullreferenceexception-when-using-invoke/598853/14 "2020-10-10T13:20:21Z")

</div>

I know its old but did you manage to fix this ? Im having the same issue where im getting NullReferenceException error on Invoke function after i reload scene for seamingly no reason. Im desperate
