Question by
TUTOCF · May 21, 2018 at 01:06 PM ·
unity 5crashwhilewhile-loop
Unity keeps crashing when 'While' is used...
Hello,
My script is very simple : once the character stops moving, the camera background will change. I don't know why, every time I start the Play mode, Unity crashes.
Here's my script (C#) :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cameraBackground : MonoBehaviour {
public Color black = Color.black;
public Camera cam;
public GameObject character;
// Use this for initialization
void Start () {
cam = GetComponent<Camera>();
}
// Update is called once per frame
void Update () {
while(character.GetComponent<Animator>().enabled == false){
cam.backgroundColor = black;
}
}
}
Any help would be appreciated.
Thank you and sorry for my English.
Comment
Best Answer
Answer by Vicarian · May 21, 2018 at 01:42 PM
Don't stick a loop that could be infinite inside another infinite loop. The Update() method is already a loop, so all you have to do is:
if (!character.GetComponent<Animator>().enabled)
cam.backgroundColor = black;