- Home /
Unity Editors crashes after using a button in scene
Hi,
My Unity Editor(2019.4.11f1) keeps crashing after I use a button in my scene. Now I don't know why but I'm pretty sure it has to do something with my Game Manager. I also tried using another version of the editor but I get the same problem. Here is my Game Manager Script:
{
public TextMeshProUGUI CounterText;
public TextMeshProUGUI scoreText;
private int turn = 0;
public static int score;
public int scoreValue;
public bool isGameActive;
public GameObject titleScreen;
public Button startbutton;
private void Start()
{
}
private void OnTriggerEnter(Collider other)
{
turn += 1;
CounterText.text = "Turn : " + turn + "/3";
score += scoreValue;
scoreText.text = "score: " + score;
}
public void startGame()
{
isGameActive = true;
score = 0;
turn = 0;
titleScreen.gameObject.SetActive(false);
}
}
Here is the Game Manager's inspector:
I'm making a title screen that i want to deactivate as soon as I click the start button, when I click the button the whole editor freezes. Can anyone make out what is causing that. I'll add the script for my button as well just in case:
private Button startButton;
private GameManager gameManager;
// Start is called before the first frame update
void Start()
{
startButton = GetComponent<Button>();
gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
startButton.onClick.AddListener(StartGameOnClick);
}
// Update is called once per frame
void Update()
{
}
void StartGameOnClick()
{
Debug.Log(" Game has started");
gameManager.startGame();
}
}
Does anybody have a clue of why this is happening? Thanks in advance!
Is there any more relevant code to this? I don't see anything that immediately stands out. An editor freeze often means getting stuck in a loop.
Does it actually crash after freezing (aka, present the "Unity Editor has stopped working!" dialog), or does it just hang indefinitely?
@toficofi As soon as i click the button it just freezes and it stays that way, I even tried to wait like 15 $$anonymous$$utes but it was still frozen, I didn't get any dialog, there was just no way I could interact with the editor anymore.
I have some other scripts, one is the PlayerController and the other FollowPlayer, that's for the camera to follow to ball with a offset. Here's the PlayerController script:
{
public float speed;
private float horizontalInput;
private float verticalInput;
//how many balls the player has
public static float ballCount = 3;
//player's score
public static float score = 0;
Rigidbody ballRb;
// checks if the ball has been dropped
bool isDropped;
private GameManager gameManager;
// Start is called before the first frame update
void Awake()
{
ballRb = GetComponent<Rigidbody>();
gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
}
// Update is called once per frame
void Update()
{ while (gameManager.isGameActive)
{
//if the ball has not been dropped, use MoveBall method
if (!isDropped)
{
MoveBall();
}
// makes the ball drop with spacebar
if (Input.GetKeyDown(KeyCode.Space))
{
isDropped = true;
ballRb.useGravity = true;
}
}
}
//makes player move the ball with WASD
void MoveBall()
{
verticalInput = Input.GetAxis("Vertical");
horizontalInput = Input.GetAxis("Horizontal");
transform.Translate(Vector3.forward * Time.deltaTime * speed * verticalInput);
transform.Translate(Vector3.right * Time.deltaTime * speed * horizontalInput);
}
}
I don't really think there is anything wrong with this script either but I could be overlooking something. Thanks for taking your time!
Answer by toficofi · Mar 23, 2021 at 01:16 AM
There's your problem:
void Update()
{ while (gameManager.isGameActive)
{ ...
You can't use a while loop like this in your Update method because Unity runs on a single thread, which means it waits until the whole Update has finished executing before moving onto the next frame, and in this case gameManager.isGameActive
stays true, and that Update() will never finish.
You are probably wanting to do this, instead:
void Update()
{ if (gameManager.isGameActive)
{
Yes, that has solved it. Looks like I misunderstood the concept of the while loop, I will take a better look at how to properly use it. Thanks a lot
Answer by pKallv · Mar 22, 2021 at 11:52 PM
Check out the Unity log to find out what the problem is, you can find where it is here: https://docs.unity3d.com/Manual/LogFiles.html
Your answer
Follow this Question
Related Questions
How do I make a photography function? 0 Answers
Slowly increase joint limit over time 1 Answer
How to return control back to the original script? 1 Answer
weighted inventory system 1 Answer
Unity crashes on start up (Linux) 0 Answers