Search Unity

Unity Freezes After Script and Has to be Terminated

Discussion in 'Scripting' started by FGPArthurVII, Feb 26, 2018.

  1. FGPArthurVII

    FGPArthurVII

    Joined:
    Jan 5, 2015
    Posts:
    106
    This is a simple script for opening and closing a door within distance, If I aproach once the door opens, if I aproach and It is open it closes. But for some reason, when I get on contact witch the door's trigger, Unity Freezes at all, to never unfreezee, the only way to stop unity when it happens is by going on the Task Manager and terminating it then restarting the engine (Of course if I get near the doors it will happen again). Why is it happening?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Door : MonoBehaviour
    6. {
    7.     public GameObject door;
    8.     private bool isClosed;
    9.     private int colCount;
    10.  
    11.     void Start ()
    12.     {
    13.         isClosed = false;
    14.         colCount = 0;
    15.     }
    16.  
    17.     void OnTriggerEnter (Collider col)
    18.     {
    19.         if (col.gameObject.tag == "Player" )
    20.         {
    21.             if (colCount % 2 == 0)
    22.             {
    23.                 while (door.transform.eulerAngles.z <= 89)
    24.                 {
    25.                     door.transform.eulerAngles += new Vector3 (0,0, 1 * Time.deltaTime);
    26.                 }
    27.             }
    28.             else if (colCount % 2 == 1)
    29.             {
    30.                 while (door.transform.eulerAngles.z >= 1)
    31.                 {
    32.                     door.transform.eulerAngles -= new Vector3 (0,0, 1 * Time.deltaTime);
    33.                 }
    34.             }  
    35.         }
    36.     }
    37. }
    38.  
    Thanks;
    Arthur
     
  2. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Its definitely the while loops. Unfortunately, the exact reason why isn't hitting me.
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Try this to see your values

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class Door : MonoBehaviour
    5. {
    6.     public GameObject door;
    7.     private bool isClosed;
    8.     private int colCount;
    9.     void Start ()
    10.     {
    11.         isClosed = false;
    12.         colCount = 0;
    13.     }
    14.     void OnTriggerEnter (Collider col)
    15.     {
    16.         if (col.gameObject.tag == "Player" )
    17.         {
    18.            StartCoroutine(MoveDoor());
    19.         }
    20. }
    21.     IEnumerator MoveDoor()
    22.     {    
    23.      if (colCount % 2 == 0)
    24.             {
    25.                 while (door.transform.eulerAngles.z <= 89)
    26.                 {
    27.                     door.transform.eulerAngles += new Vector3 (0,0, 1 * Time.deltaTime);
    28.                     print(door.transform.eulerAngles.z);
    29.                     yield return null;
    30.                 }
    31.             }
    32.             else if (colCount % 2 == 1)
    33.             {
    34.                 while (door.transform.eulerAngles.z >= 1)
    35.                 {
    36.                     door.transform.eulerAngles -= new Vector3 (0,0, 1 * Time.deltaTime);
    37.                     print(door.transform.eulerAngles.z);
    38.                     yield return null;
    39.                 }
    40.             }
    41.     }
    42. }