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

Distance script not working

Discussion in 'Editor & General Support' started by TheWgames, Apr 3, 2014.

  1. TheWgames

    TheWgames

    Joined:
    Nov 29, 2012
    Posts:
    27
    I am busy with making a script to display the distance to a certain object to test something.

    I made this script, and it worked.
    -----------------------------------------------------------------------------------------------------------
    var ReyHound : Transform;
    if (ReyHound)
    {
    var dist = Vector3.Distance(ReyHound.position, transform.position);
    print ("Distance to Destination: " + dist);
    }
    ----------------------------------------------------------------------------------------------------------------------------------
    But it only prints the distance in the beginning of the game and I want it to update.
    So I tried a while statement like this:
    ---------------------------------------------------------------------------------------------------------------------------------
    var ReyHound : Transform;
    while (1>0) {
    if (ReyHound)
    {
    var dist = Vector3.Distance(ReyHound.position, transform.position);
    print ("Distance to Destination: " + dist);
    }
    }
    -----------------------------------------------------------------------------------------------------------------------------------
    But now Unity crashes constantly if I play the game.

    Can somebody please tell me what I'm doing wrong?
    Thanks already.
     
  2. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
    this line is the problem:
    while (1>0) {


    ..infinite loop -> crash

    if you wish to use while loop like that, you need to use coroutine (search forum or read docs). Easier one: just place your dist / print part of the code to Update instead of Start (i suppose its in Start now).

    And in future, use code tags when posting code.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,745
    I presume that that is the script in its entirety, which in Javascript, means that it is assumed to be in Start().

    Code (csharp):
    1.  
    2. var ReyHound : Transform;
    3. void Update() {
    4. if (ReyHound)
    5. {
    6. var dist = Vector3.Distance(ReyHound.position, transform.position);
    7. print ("Distance to Destination: " + dist);
    8. }
    9. }
    10.  
     
  4. TheWgames

    TheWgames

    Joined:
    Nov 29, 2012
    Posts:
    27
    Thanks! It's working.
    I just had to replace the void with function, but it works perfectly!:D