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:
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)
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
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.
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