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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Script making unity stop responding(JS)

Discussion in 'Scripting' started by Fireking883, Jul 16, 2015.

  1. Fireking883

    Fireking883

    Joined:
    Nov 28, 2014
    Posts:
    50
    So I have been working on a simple script to make my character float on water, but whenever I run the script, Unity stops responding. I'm sure there is a better way to write the script, so if you know on,e help would be appreciated, but I just need to know why Unity is crashing.
    Code (JavaScript):
    1. #pragma strict
    2. var Player : GameObject;
    3. var InWater : boolean = false;
    4. function Start(){
    5.     Player = this.gameObject;
    6. }
    7. function Update(){
    8.     while(InWater == true){
    9.         Player.GetComponent.<Rigidbody>().AddForce(Vector3.up * 2 * Time.deltaTime);
    10.     }
    11. }
    12. function OnTriggerEnter(other : Collider){
    13.     if (other.gameObject.tag == "water"){
    14.         Debug.Log("InWater");
    15.         InWater = true;
    16.     }
    17. }
    18. function OnTriggerExit(other : Collider){
    19.     if (other.gameObject.tag == "water"){
    20.         Debug.Log("OutWater");
    21.         InWater = false;
    22.     }
    23. }
    24.  
    I have tried changing the AddForce(Vector3.up*2*Time.deltaTime); line to different numbers, even removing it, but it wont work. Thanks in advance!
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Code (JavaScript):
    1. #pragma strict
    2. var Player : GameObject;
    3. var InWater : boolean = false;
    4. function Start(){
    5.     Player = this.gameObject;
    6. }
    7. function Update(){
    8.     if(InWater == true){
    9.         Player.GetComponent.<Rigidbody>().AddForce(Vector3.up * 2 * Time.deltaTime);
    10.     }
    11. }
    12. function OnTriggerEnter(other : Collider){
    13.     if (other.gameObject.tag == "water"){
    14.         Debug.Log("InWater");
    15.         InWater = true;
    16.     }
    17. }
    18. function OnTriggerExit(other : Collider){
    19.     if (other.gameObject.tag == "water"){
    20.         Debug.Log("OutWater");
    21.         InWater = false;
    22.     }
    23. }
    24.  
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    most of the time you don't need while loops in update, update is already a game loop.
     
  4. Fireking883

    Fireking883

    Joined:
    Nov 28, 2014
    Posts:
    50
    My error there! I usually don't use while loops, so I just stuck it in there. Thanks for the help!