Search Unity

SpriteText Update from a second script

Discussion in 'Scripting' started by Danim3D, Jul 11, 2011.

  1. Danim3D

    Danim3D

    Joined:
    Sep 7, 2009
    Posts:
    18
    I don't understand why this doesn't work? I am trying to update a SpriteText from a second script but I get this error:

    NullReferenceException: Object reference not set to an instance of an object
    myScript.Start() (at Assets/Scripts/myScript.cs


    myText.cs
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class myText : MonoBehaviour{
    5.     public static myText Instance;
    6.     public SpriteText txt;
    7.     void Start(){
    8.         updateSpriteText();
    9.     }
    10.     public void updateSpriteText(){
    11.         txt.Text = "myText";
    12.     }
    13. }
    myScript.cs
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class myScript : MonoBehaviour{
    6.     void Start(){
    7.         myText.Instance.updateSpriteText();
    8.     }
    9. }
    10.  

    Thanks!
     
    Last edited: Jul 11, 2011
  2. oneXwork

    oneXwork

    Joined:
    Mar 3, 2011
    Posts:
    266
    you nvr assign anything to Instance that why it produce the error
    Set the Instance to the script in Start() that shld fixed it
     
  3. Danim3D

    Danim3D

    Joined:
    Sep 7, 2009
    Posts:
    18
    I have already assign the SpriteText txt in the Inspector.
    This is already what I am doing in Start(), it doesn't work.
    But it should work anywhere, in myScript.cs I just want to call the updateSpriteText() from the script myText.cs.
     
  4. oneXwork

    oneXwork

    Joined:
    Mar 3, 2011
    Posts:
    266
    i think this shld solve the problem:
    Instance = new myText(); in the start()
     
  5. Danim3D

    Danim3D

    Joined:
    Sep 7, 2009
    Posts:
    18
    Thank, with Instance = this; there is no NullReferenceException error anymore.
    But the call of this function bellow is still not executing, nothing happen.
    What else could I have miss?

    myText.Instance.scoresUpdate();

    myText.cs
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class myText : MonoBehaviour{
    5.      public static myText Instance;
    6.      public SpriteText txt;
    7.      public int scores= 0;
    8.      void Awake(){        
    9.          Instance = this;
    10.      }
    11.      void Start(){
    12.          updateSpriteText();
    13.      }
    14.      public void updateSpriteText(){
    15.          print("Enter") + scores;
    16.          txt.Text = "Scores: " + scores;
    17.      }
    18.      public void scoresUpdate(){
    19.          ++scores;
    20.          updateSpriteText();
    21.      }
    22. }
    myScript.cs
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class myScript : MonoBehaviour{
    5.      public static myScript Instance;
    6.      void Awake(){        
    7.          Instance = this;
    8.      }
    9.      void Update(){
    10.         if(Input.GetKeyDown("space")){
    11.              myText.Instance.scoresUpdate();
    12.         }
    13.      }
    14. }
     
    Last edited: Jul 15, 2011
  6. oneXwork

    oneXwork

    Joined:
    Mar 3, 2011
    Posts:
    266
    try print("Enter"); in the updateSpriteText() if it enter, the problem is with the txt
    maybe try txt = new SpriteText()
     
  7. Danim3D

    Danim3D

    Joined:
    Sep 7, 2009
    Posts:
    18
    Thank! with the print("Enter"); I saw that the problem was with the SpriteText.
    I could not assign the same SpriteText in the Inspector that has the myText.cs script.
    The only way I am able to make it work is by creating two SpriteText game object that refer each other in the Inspector myText.cs script.

    Maybe there is an another way by using a single SpriteText...
     
    Last edited: Jul 15, 2011