Skip to main content
Commonmark migration
Source Link

I'm using a textmesh to check variables in real time. Thing is if I use AddComponent within Update(), it obviously starts spamming and slows the game. If I try with GetComponent the error is the following:

NullReferenceException: Object reference not set to an instance of an object

 

DebugCombo.Update() (at Assets/DebugCombo.js:9)

which points to the text.text assignment.

This is the script from DebugCombo.js, from which I call the Combo class (from Combo.js)

#pragma strict

public class DebugCombo extends MonoBehaviour{

    function Update () {
        var text = gameObject.GetComponent(TextMesh);
        var combo = gameObject.GetComponent(Combo);
    
        text.text = "Current: "+ Time.time +" | Limit: " + combo.comboTime;
    }

}

How do I get rid of this error?

I'm using a textmesh to check variables in real time. Thing is if I use AddComponent within Update(), it obviously starts spamming and slows the game. If I try with GetComponent the error is the following:

NullReferenceException: Object reference not set to an instance of an object

 

DebugCombo.Update() (at Assets/DebugCombo.js:9)

which points to the text.text assignment.

This is the script from DebugCombo.js, from which I call the Combo class (from Combo.js)

#pragma strict

public class DebugCombo extends MonoBehaviour{

    function Update () {
        var text = gameObject.GetComponent(TextMesh);
        var combo = gameObject.GetComponent(Combo);
    
        text.text = "Current: "+ Time.time +" | Limit: " + combo.comboTime;
    }

}

How do I get rid of this error?

I'm using a textmesh to check variables in real time. Thing is if I use AddComponent within Update(), it obviously starts spamming and slows the game. If I try with GetComponent the error is the following:

NullReferenceException: Object reference not set to an instance of an object

DebugCombo.Update() (at Assets/DebugCombo.js:9)

which points to the text.text assignment.

This is the script from DebugCombo.js, from which I call the Combo class (from Combo.js)

#pragma strict

public class DebugCombo extends MonoBehaviour{

    function Update () {
        var text = gameObject.GetComponent(TextMesh);
        var combo = gameObject.GetComponent(Combo);
    
        text.text = "Current: "+ Time.time +" | Limit: " + combo.comboTime;
    }

}

How do I get rid of this error?

Source Link
Fabián
  • 285
  • 3
  • 6
  • 12

Error when using GetComponent (Object reference not set to an instance of an object)

I'm using a textmesh to check variables in real time. Thing is if I use AddComponent within Update(), it obviously starts spamming and slows the game. If I try with GetComponent the error is the following:

NullReferenceException: Object reference not set to an instance of an object

DebugCombo.Update() (at Assets/DebugCombo.js:9)

which points to the text.text assignment.

This is the script from DebugCombo.js, from which I call the Combo class (from Combo.js)

#pragma strict

public class DebugCombo extends MonoBehaviour{

    function Update () {
        var text = gameObject.GetComponent(TextMesh);
        var combo = gameObject.GetComponent(Combo);
    
        text.text = "Current: "+ Time.time +" | Limit: " + combo.comboTime;
    }

}

How do I get rid of this error?