RetroAnt
RetroAnt

Reputation: 48

Unity Crashes when Using this UI

using UnityEngine.UI; using UnityEngine;

public class Tutorial : MonoBehaviour { public Text tutorialText;

void Update()
{
    tutorialText.text = "Press [SPACE] to Jump... (obviously...)";
    if (Input.GetKeyDown(KeyCode.Space))
    {
        while (true)
        {
            tutorialText.text = "Now Shoot";
            if (Input.GetKeyDown(KeyCode.Mouse0))
            {
                while (true)
                {
                    tutorialText.text = "Nice";
                }
            }
        }
    }
}

}

Upvotes: 0

Views: 113

Answers (1)

IndieGameDev
IndieGameDev

Reputation: 2974

Change your code to that, because right now you are in a infinite loop which sets your tutorialtext to "Now Shoot" again and again until Unity crashes.

if (Input.GetKeyDown(KeyCode.Space))
{
   tutorialText.text = "Now Shoot";
}
else if (Input.GetKeyDown(KeyCode.Mouse0))
{
   tutorialText.text = "Nice";
}

If you want the text to disappear after the first time you done the actions you should add bool variables.

bool jumped = false;
bool hasShot = false;

And then implement thoose bools into your code like this:

if (Input.GetKeyDown(KeyCode.Space))
{
   if (jumped)
      return;

   tutorialText.text = "Now Shoot";
   jumped = true;
}
else if (Input.GetKeyDown(KeyCode.Mouse0))
{
   if (hasShot || !jumped)
      return;

   tutorialText.text = "Nice";
   hasShot = true;
}

You also need to move the first text definition out of Update, because Update is called each frame. Put it into void Start() instead

void Start() {
   tutorialText.text = "Press [SPACE] to Jump... (obviously...)";
}

Upvotes: 0

Related Questions