0

I can't display text and I get a NullReferenceException when the method is executed. On top of that the code doesn't stop running as it should.

// Use this for initialization
void Start()
{
    // Default position not valid? Then it's game over
    if (!isValidGridPos())
    {
        Text text;
        text = GetComponent<Text>();

        text.text = "Game Over";
        Destroy(gameObject);
        if (Input.GetKeyDown(KeyCode.R))
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
        }

    }
}

The code comes from this tutorial. I just wanted to add it a restart and a Game Over message.

EDIT: Trying to display text with text = GetComponent() is not working. What's another way to display text on unity that actually works? I tried GUIText too but I can't make it visible on display.

derHugo
  • 83,094
  • 9
  • 75
  • 115
ffuentes
  • 1,042
  • 5
  • 16
  • 36
  • Which line exactly is the error being thrown on? Are you sure it's not coming from within `isValidGridPos()`? – Serlite Mar 07 '17 at 22:11
  • When I add a message to "text". – ffuentes Mar 07 '17 at 22:13
  • 2
    I see, you've edited the code in. Alright, are you sure you have a Text component attached to the object that this script is also attached to? – Serlite Mar 07 '17 at 22:15
  • Yeah, there it is http://imgur.com/a/VV4XN – ffuentes Mar 07 '17 at 22:18
  • Did you attach the script with this code from your question to the *GameOver* GameObject? – Programmer Mar 07 '17 at 22:24
  • Oh...er, `Destroy(gameObject);` gets called right before - that sets references to the object to null, and probably its components too. Perhaps do it after? Although your code won't perceptibly do much, since you're destroying the object and reloading the scene as soon as you set its text... – Serlite Mar 07 '17 at 22:25
  • @Serlite That shouldn't be the problem. The script/GameObject is destroyed next frame. – Programmer Mar 07 '17 at 22:26
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Hellium Mar 07 '17 at 22:32
  • It's not Destroy. The code doesn't recognize text.text message even if there's no destroy code. – ffuentes Mar 07 '17 at 22:40
  • Most probably it's because of what Programmer said. – Galandil Mar 08 '17 at 00:14
  • But I played around with it even removing Destroy altogether and it doesn't recognize it. Is there another way to display text? I tried GUIText but I can't see it. – ffuentes Mar 08 '17 at 00:33
  • 1
    To which game object did you attach the script? It needs to be attached to the GameOver game object, if not, you'll get the exception. – Galandil Mar 08 '17 at 01:08
  • Ok, the point is that this script is tied to prefabs. – ffuentes Mar 08 '17 at 01:25
  • The screenshot you posted doesn't show the script and Text on the same gameobject, it only shows you have a Text component on the `GameOver` object. Please confirm your script is on the 'GameOver` script too – user2590928 Aug 09 '17 at 23:57

3 Answers3

0

I have looked at the tutorial and they have the following code:

if (!isValidGridPos()) {
    Debug.Log("GAME OVER");
    Destroy(gameObject);
}

This works because it prints to the debug log console. You obviously want to capture that and print it on the screen. This requires a canvas and a text component being added to the gameobject the script is on. Declaring a text variable here won't add one to the game object, it has to be done in the unity inspector.

The following code:

if (Input.GetKeyDown(KeyCode.R))
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

Shouldn't be here, as this object will already be destroyed this code will be rendered useless unless you are holding the key down in the same frame that the object is created. It should instead be on in another script on a gameobject that won't be destroyed and it will await an r key to reload the level.

Mr.Bigglesworth
  • 924
  • 9
  • 22
0

If text = GetComponent<Text>(); isn't working it could be because your Text component is not on the same gameobject that your script is attached to.

In the Unity Editor, check that the text component and your script are on the same gameobject

user2590928
  • 176
  • 10
-2

Did you inspect editor view while running?
Don't use destroy in Strar(). You can set delay like so Destroy(gameobject, 5.0f) and use it in some other callback function (ex. Action).

Now you just destroying whole gameobject all its scripts and components(including Text) right after it initialized. If it is in scene from beginning, then it destroyed as soon as you press Play.

Start() runs just once so what does isValidGridPos() doing?

maximelian1986
  • 2,308
  • 1
  • 18
  • 32