Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Unity Freeze

Discussion in '2D' started by Orinuvan, Mar 24, 2019.

  1. Orinuvan

    Orinuvan

    Joined:
    Feb 3, 2019
    Posts:
    64
    Hi, so I wrote this piece of code to my project and when Shield is true, in tha game the game freezes and the only way to unfreeze is to fore quit unity, so what tf did I did?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Hands : MonoBehaviour
    6. {
    7.     public Transform ManaBar;
    8.     public bool Shield;
    9.     public Animator MyAnimator;
    10.     IEnumerator WaitAndPrint()
    11.     {
    12.         yield return new WaitForSeconds(0.1f);
    13.         ManaBar.localScale -= new Vector3(0.1f, 0f, 0f);
    14.     }
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         MyAnimator = GetComponent<Animator>();
    20.  
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.      
    27.         if (Input.GetMouseButtonDown(1))
    28.         {
    29.             Shield = true;
    30.  
    31.  
    32.         }
    33. while (Shield == true)
    34.         {WaitAndPrint();
    35.          
    36.         }
    37.  
    38.        
    39.         MyAnimator.SetBool("Shield", Shield);
    40.  
    41.  
    42.     }
    43. }
    44.  
     
  2. RidgeWare

    RidgeWare

    Joined:
    Apr 12, 2018
    Posts:
    65
    You've got an infinite while loop there. It's just running that bit of code endlessly. Unity Editor doesn't cope at all well with infinite loops.

    Also, I'd never personally put a while loop inside Update.
     
    Orinuvan likes this.