Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jacobsc · Dec 09, 2019 at 04:26 AM · if-statementswhilewhile-loopwhile loopinactive

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jacobsc · Dec 09, 2019 at 08:48 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

118 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges