Search Unity

Why does this simple script causes Unity to crash?

Discussion in 'Scripting' started by glom1envisage0, Nov 12, 2011.

  1. glom1envisage0

    glom1envisage0

    Joined:
    Apr 3, 2011
    Posts:
    167
    Code (csharp):
    1.  
    2. //Will this cause unity to crash everytime?
    3.  
    4. var SomeObject : GameObject;
    5. //Use any object to fill in this variable in the editor.
    6.  
    7. function Start(){
    8. VibrateObject();
    9. }
    10.  
    11. function VibrateObject(){
    12. SomeObject.transform.position.x += 0.0000001;
    13. SomeObject.transform.position.x -= 0.0000001;
    14. Reset_VibrateObject();
    15. }
    16.  
    17. function Reset_VibrateObject(){
    18. VibrateObject();
    19. }
    20.  
    I have created "loops" this way before, but something about this doesn't work, any ideas?
     
  2. Diviner

    Diviner

    Joined:
    May 8, 2010
    Posts:
    677
    This one however is infinite. All infinite loops must yield at least one frame before executing again.

    on your Reset_VibrateObject function add

    yield;

    before VibrateObject();

    It should fix the crash.
     
  3. glom1envisage0

    glom1envisage0

    Joined:
    Apr 3, 2011
    Posts:
    167
    Thanks, I noticed you suggested for someone to use a for loop in the Update function in a question asked earlier. I was able to do the same thing by having the forward and back motion placed in a for loop so now I have two solutions. Thanks, I was needing my character to move by just a smidgen every frame to interact with some repeating functions based on OnTriggerEnter. The trigger for one in particular was attached to an animating box that should move the character when it comes in contact with him but, the movement was only happening when the character moved into it. This way, the character is still moving into it, without seeming to move.
     
  4. vreference

    vreference

    Joined:
    Mar 22, 2011
    Posts:
    154
    Just this might be better:

    Code (csharp):
    1.  
    2. Update()
    3. {
    4. SomeObject.transform.position.x += Random.Range(-.0000002, .000002);
    5. }
    6.  
    Although the object will drift with just this one line.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (csharp):
    1. function VibrateObject(){
    2. SomeObject.transform.position.x += 0.0000001;
    3. SomeObject.transform.position.x -= 0.0000001;
    4. Reset_VibrateObject();
    5. }
    6.  
    7. function Reset_VibrateObject(){
    8. VibrateObject();
    9. }
    This is infinite recursion, which will eventually crash due to stack overflow even if you put a yield in there somewhere. (This is a moot point, but: there's no point having the Reset_VibrateObject function, which does nothing but call the first function. You could cut out the middle man, so to speak, and just call the first function directly. It's still infinite recursion and should be avoided.) Instead, use a loop in a coroutine:

    Code (csharp):
    1. function Start(){
    2.     while (true) {
    3.         someObject.transform.position.x += 0.0000001;
    4.         yield;
    5.         someObject.transform.position.x -= 0.0000001;
    6.         yield;
    7.     }
    8. }
    --Eric