Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved This Script CRASHES Unity

Discussion in 'Editor & General Support' started by Simplisticated_Development, Sep 23, 2023.

  1. Simplisticated_Development

    Simplisticated_Development

    Joined:
    Jun 26, 2023
    Posts:
    61
    Hey everyone-

    So I have been scripting a system to make a siren. My idea is that a lit block moves back and forth when a key is pressed. The key "F" is the siren's on/off switch. Here is my script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Threading;
    4. using UnityEngine;
    5.  
    6. public class Sirens1 : MonoBehaviour
    7. {
    8.     private bool Switch = false;
    9.  
    10.     void Start()
    11.     {
    12.        
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         if (Input.GetKeyDown(KeyCode.F)) {
    18.  
    19.             Switch = !Switch;
    20.  
    21.             if (Switch == true) {
    22.                 transform.Translate(10, 0, 0);
    23.                 Invoke("Lights", 1);
    24.             }
    25.  
    26.         }
    27.  
    28.      
    29.     }
    30.  
    31.     void Lights()
    32.     {
    33.         Debug.Log("Hello!");
    34.  
    35.         transform.Translate(-10, 0, 0);
    36.     }
    37. }
    The idea is that the light block will move and then wait. When I use "IEnumerator", this script doesn't work. So for the waiting system, I used "Invoke"

    This script works okay, but you have to press "F" every time you want the siren to move. The intended function is pressing "F" once and the siren keeps going back and forth. So naturally, I change the "If" statement to a "While" statement.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Threading;
    4. using UnityEngine;
    5.  
    6. public class Sirens1 : MonoBehaviour
    7. {
    8.     private bool Switch = false;
    9.  
    10.     void Start()
    11.     {
    12.        
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         if (Input.GetKeyDown(KeyCode.F)) {
    18.  
    19.             Switch = !Switch;
    20.  
    21.             while (Switch == true) {
    22.                 transform.Translate(10, 0, 0);
    23.                 Invoke("Lights", 1);
    24.             }
    25.  
    26.         }
    27.  
    28.      
    29.     }
    30.  
    31.     void Lights()
    32.     {
    33.         Debug.Log("Hello!");
    34.  
    35.         transform.Translate(-10, 0, 0);
    36.     }
    37. }
    This would obviously be the answer. Ideally, the light block keeps moving back and forth once you press "F". But when I run the game, Unity crashes. This game has very few other scripts, and the app only breaks when I use "when" instead of "IF" in that one section.

    I don't know WHY this happens, but it might be similar to how using "SLEEP" in Unity tends to crash the app. If someone could help me understand what is going wrong and provide me with a script that will have the Light block move back and forth when your press "F". When you click "F" again, the light block stops.

    Thanks in advance.
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,834
    When your code is in a loop, NOTHING else can happen in your C# code. You don't get more Update() calls in your class here, and nothing else gets Update() calls or any other chances to run, either. Since you don't change the condition of the
    while
    block inside the block, then once the condition is true it will be true forever. Unity will continue to run the loop until the OS is told to kill the process.
     
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,955
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,834
    What you actually want is very close to your first version:

    Code (CSharp):
    1. void Update()
    2. {
    3.     if (Input.GetKeyDown(KeyCode.F)) {
    4.         Switch = !Switch;
    5.     }
    6.     if (Switch == true) {
    7.         Invoke("Lights", 1);
    8.     }
    9. }
    You want to process the "if it's good to control the light" condition outside the moment of pressing the key. I just moved your second
    if
    condition to follow the key handling part.
     
  5. Simplisticated_Development

    Simplisticated_Development

    Joined:
    Jun 26, 2023
    Posts:
    61
    thanks for your replies! I have solved the problem now, thanks to your help!
     
  6. Simplisticated_Development

    Simplisticated_Development

    Joined:
    Jun 26, 2023
    Posts:
    61
    thanks for your replies! I have solved the problem now, thanks to your help! (I also understand why it broke)