when I press the play button the Unity Editor crashes and I need to close it with the task manager to work again! can someone help me solve this? thanks in advance
You probably have an infinite loop in your code. Did you work on any scripts recently?
this happens when i use a text with scorepoints
Here´s the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
public GameObject player;
public Text Score;
public float points;
public float pointspersecond;
// Start is called before the first frame update
void Start()
{
points = 0f;
pointspersecond = 1f;
}
// Update is called once per frame
void Update()
{
while (player.GetComponent<PlayerEnergy>().Health > 0)
{
Score.text = (int)points + "";
points += pointspersecond * Time.deltaTime;
}
}
}
As I suspected, you have an infinite loop. You have a while loop that will never exit. Why won’t it exit? It’s checking Health on the PlayerEnergy component. If that value is greater than zero, the code inside the block will run. The code inside the block never changes that health value. So the while condition continues to be true and the code in the block runs over and over again, forever.
That’s what while loops do. They keep running as long as their condition is true. They don’t just run once per frame (that’s what Update already does), They have no concept of frames. They know only about the condition you put. If the condition you put doesn’t change inside the loop, it will never stop.
Whenever your code is running in Unity the entire game engine is waiting for your code to finish running so it can continue doing game engine things, like starting the next frame. If your code never stops running, the game engine can never continue doing game engine things.
Are you sure you don’t just want an if statement instead of a while loop? If statements will only run once if the condition is true and then allow the program to continue
In this case, I think you just want to change the “while” to “if”. Update already runs once every frame, and it looks like you’re trying to accumulate player score every frame the player has positive health.