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 /
  • Help Room /
avatar image
0
Question by matt1025 · Apr 19, 2016 at 08:17 PM · gamefreezewhile-loopkey pressed

Bug in loop while, and Event with Keyboard

Hi guys !

I want to hide a player in a cube which call Forest when he press "E". Then if the player hit again "E" he quits the Cube Forest to pop at a location.

I tried so many times to do that but I don't know why, my loop while freeze my game, and I don't find how can I assign the key "E" for this two events.

Here is my code (he doesn't work, but if you can give me some advices pleaaaase and why my game freeze ? )

THANKS THANKS A LOT !

PlayerMovement :

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
     public Forest forest;
     public int moveSpeed;
     public Vector3 playerPosition;
     public Vector3 positionTransform;
     
     public bool inForest;
         
     
 
    
     void Start () {
 
         forest = forest.GetComponent<Forest>();
         inForest = false;
     
     }
 
     
     void Update()
     {
         // CHARACTER MOVEMENT
         transform.Translate(Input.GetAxisRaw("Horizontal") * Time.deltaTime * moveSpeed, 0f, 0f);
         transform.Translate(0f, 0f, Input.GetAxisRaw("Vertical") * Time.deltaTime * moveSpeed);
         playerPosition = GameObject.Find("Player").transform.position;
 
         //GO FOREST
         while (inForest == true && Input.GetKeyDown("e"))
         {
 
             forest.HideForest();
          
             if (Input.GetKeyDown("e"))
             {
                 forest.ExitForest();
             }  
         }
 
     }
 
  
 //COLLIDER  
 
 void OnTriggerEnter(Collider other)
     {
         if (other.transform.tag == "Forest")
         {
             inForest = true;
         }
 
     }

 }

Here is my Forest Code :

 using UnityEngine;
 using System.Collections;
 
 public class Forest : MonoBehaviour {
 
     public Vector3 playerForest;
     public Vector3 exitPos;
     
 
         public void HideForest()
         {
             //PLAYER IS HIDDIND TO THIS VECTOR
             playerForest = new Vector3(20, -1, -5);
             GameObject.Find("Player").transform.position = playerForest;
                    
          }
 
         public void ExitForest()
     {
         if (Input.GetKey("e"))
         {
             
             exitPos = GameObject.Find("ForestExit").transform.position;
             GameObject.Find("Player").transform.position = exitPos;
 
         }
     }
                   
     }
 
 



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

1 Reply

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

Answer by incorrect · Apr 19, 2016 at 08:24 PM

You should understand that the code you place into Update() method is executed not separately from the game engine working but instead it is executed with every frame. So the game proceeds to the next frame only after all updates were made.

In your case you are doing while (inForest == true && Input.GetKeyDown("e")) which causes an infinite loop. Both those bools will never change because inForest is not changed inside your "while" loop and Input.GetKeyDown can change only during next frame.

To make things that are supposed to be executed through several frames you should use different approaches. Like coroutins or multithreading (the last one is for experienced programmers due to some Unity-specific limitations).

But in your case there is no need to use them.

Comment
Add comment · Show 2 · 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 matt1025 · Apr 19, 2016 at 09:59 PM 0
Share

Thanks man, i'm gonna find another approache !

avatar image incorrect matt1025 · Apr 19, 2016 at 10:06 PM 0
Share

Something like that should work.

 void Update()
 {
     if(Input.Get$$anonymous$$eyDown("e"))
     {
         if(inForest)
             Exit();
         else Hide();
     }
 }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity is freezing when I change my script, what can I do? [SOLVED] 1 Answer

[Solved]Cant Find loop that freezes Unity 2 Answers

While Loop Freezing Unity 1 Answer

Why does this while loop freeze unity? 1 Answer

Unity Crashes When Clicking Play: Script Error? 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