NullReferenceException when using Invoke

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:

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!

1 Like

can you tell at which point exactly the nullref appears ?

Can you post the full error and also use code tags?
My guess would be that constructionGo or currentTower is null.

1 Like

Thanks for quick responses :slight_smile:
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)

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)

1 Like

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.

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

1 Like

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

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.

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.

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.

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.

1 Like

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.

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

1 Like