- Home /
How to get something to be inactive while there is a certain value
Ok, so I was making a script that would remove a door when I collected an item. But I want it to be reactivated when I have a different item on. (Green/red items for green/red doors). Well, I used a while script and when the player collected an item unity stopped working and I had to use task manager to shut it down.
using UnityEngine;
using System.Collections;
public class Collection : MonoBehaviour
{
public Transform tForm;
public GameObject gObject;
public GameObject redDoor;
public GameObject greenDoor;
private bool isGreen = false;
private bool isRed = false;
void OnTriggerEnter(Collider other)
{
//green
if (other.gameObject.tag == "GreenCube")
{
other.gameObject.SetActive(false);
isGreen = true;
ChangeColor();
OpenDoor();
}
//red
if (other.gameObject.tag == "RedCube")
{
other.gameObject.SetActive(false);
isRed = true;
ChangeColor();
OpenDoor();
}
}
private Color childColorGreen;
private Color childColorRed;
public void ChangeColor ()
{
childColorGreen = new Color(0, 1, 0, 1);
childColorRed = new Color(1, 0, 0, 1);
if (isGreen == true)
{
transform.Find("Body").gameObject.GetComponent<Renderer>().material.color = childColorGreen;
transform.Find("Front").gameObject.GetComponent<Renderer>().material.color = childColorGreen;
}
if (isRed == true)
{
transform.Find("Body").gameObject.GetComponent<Renderer>().material.color = childColorRed;
transform.Find("Front").gameObject.GetComponent<Renderer>().material.color = childColorRed;
}
}
public void OpenDoor()
{
while (isGreen == true)
{
greenDoor.SetActive(false);
}
while (isRed == true)
{
redDoor.SetActive(false);
}
}
}
How can I do this?
Answer by revolute · Dec 09, 2019 at 07:43 AM
Using while loops like that will freeze unity. You need to let go to allow others to update.
Since your script is based on triggers, why even bother with the while loop. If you set something's active state to false, it will stay false unless you set it to true.
Changing while(isGreen == true)
to if(isGreen == true)
should suffice.
If you truly wish to do loops, then you must call it over multiple frames, not in a single frame. Put OpenDoor in Update and change it to ifs instead. Or use coroutines.
Answer by Ermiq · Dec 09, 2019 at 11:13 AM
As @revolute said.
Just a little clarification. while
is a pretty dangerous thing for novices. Those who just started to learn programming very often don't understand how the code works, particularly they don't understand the code execution order and how the execution 'flows' through the code.
The thing is any code must be executed fast enough to let other code to be executed too, computers can't execute all things separately (although there are such things as asynchronous methods and parallel threads, but it's not the case), all code in all programs must be eventually finished to let next things to be evaluated too.
Any loop keywords (such as while
, for
, do while
, foreach
) makes the program to return to the beginning of the loop over and over again and it doesn't release the execution order to continue the program cycles untill the loop is finished. That's why every loop has to have a threshold point where it must stop eventually and let the computer to evaluate next things.
When you use while (something == true)
you tell the computer to repeat the code inside the while
scope over and over and over again while the condition has value true
, and you don't let the process to do any other stuff during this loop. That's why the process is getting freezed. It just can't continue the execution, because it's stuck within the while
loop and can't get away.
while
loops must be used carefully, you need to understand what you're doing when you consider to use it.
Basically just remember, never ever use while
with (something == true)
if you're not going to set it to false
right here in the current scope.
For example:
The following is the safe way to use the while
:
while (something == true) {
int i = 1 + 1;
something = false;
}
Here the program process knows that the condition (`something == true`) will be set to false
after some calculations and the code is able to set the condition to false
because this string is right here inside this loop that is repeated again and again.
In your code the process will never get the condition false
because the execution order is kind of stuck within the loop scope and if the condition is not going to be reset here, it won't be reset at all, because while the loop is processing any other code is not accessible and it doesn't matter if you were going to set the condition to false
somewhere else, it won't be reset.
Thanks. I studied basic java on khan academy for fun a couple of years back and I remembered there was something with the while loop that can freeze everything but I forgot what it was. Guess I found out
Your answer
Follow this Question
Related Questions
Using a while loops to repeatedly execute a method? 2 Answers
Quaternion.Slerp issue 1 Answer
Iterator variable not increasing. 1 Answer
While loop freezes unity 3 Answers
Vector3.MoveTowards becomes instant in while loop or freezes unity 1 Answer