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;

        }
    }
                  
    }

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.