Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Reload level bug?

Discussion in 'Scripting' started by wadek3, Apr 15, 2013.

  1. wadek3

    wadek3

    Joined:
    Jan 16, 2013
    Posts:
    59
    The level plays through fine the first time. However, once the level reloads the notStartedTxture and the loseTxture get stuck in a loop. One loads and then the other.

    Here is my Gui script.

    Code (csharp):
    1. #pragma strict
    2.  
    3. var notStartedTxture : Texture2D;
    4. var loseTxture : Texture2D;
    5. var winTxture : Texture2D;
    6. static var notStarted : boolean;
    7. static var missedTarget : boolean;
    8. static var hitTarget : boolean;
    9.  
    10. //var movTexture : MovieTexture;
    11. var fireSquare : GameObject;
    12. var fireSpwn : GameObject;
    13.  
    14. var Target : GameObject;
    15. var targetSpwn : GameObject;
    16. static var startTime : float;
    17. static var guiTime : float;
    18.  
    19. function Start()
    20. {  
    21.  notStarted = true;
    22.  missedTarget = false;
    23.  hitTarget = false;
    24. }
    25.  
    26. function Update()
    27. {
    28.     startTime = Time.time;
    29.     if (fireButton.firing == true)
    30.         {
    31. ///////////    
    32.         //if (movTexture.isPlaying)
    33.         //  {
    34.         //  movTexture.Pause();
    35.             //}    
    36.             }
    37.                                                
    38. }      
    39.        
    40. function OnGUI()
    41.     {
    42.         if (notStarted == true)
    43.         {
    44.             if(GUI.Button (Rect (0, 0, 1024*2, 768*2), notStartedTxture))
    45.                 {
    46. //////////             
    47. //              renderer.material.mainTexture = movTexture;
    48. //////////         
    49.             //  movTexture.Play();
    50.                 notStarted = false;
    51.                 var cloneTargetA : GameObject = Instantiate(Target,targetSpwn.transform.position, Target.transform.rotation) as GameObject;
    52.                 var cloneFireA : GameObject = Instantiate(fireSquare,fireSpwn.transform.position, fireSquare.transform.rotation) as GameObject;
    53.                 guiTime = startTime;
    54.                 print(guiTime);
    55.                 }
    56.         }  
    57.  
    58.     if (missedTarget == true)
    59.         {
    60.             if(GUI.Button (Rect (0, 0, 1024*2, 768*2), loseTxture))
    61.                 {
    62.                 Application.LoadLevel (Application.loadedLevel);
    63.                 missedTarget=false;
    64.                 }
    65.         }
    66.            
    67.     if (hitTarget == true)
    68.         {
    69.             if(GUI.Button (Rect (475*2, 150*2, 512*2, 384*2), winTxture))
    70.                 {
    71.                 Application.LoadLevel (Application.loadedLevel);
    72.    
    73.                 }          
    74.         }
    75.                    
    76. }
    77.  
    Here is the script that triggers the win / loss state.

    Code (csharp):
    1. #pragma strict
    2.  
    3. function OnTriggerEnter (col : Collider)
    4. {
    5.     if (col.tag == "shot")
    6.     {
    7.     yield WaitForSeconds (4);
    8.     gameGUI.hitTarget=true;
    9.     Destroy(this.gameObject);
    10.     }
    11. }
    12.  
    13. function Update()
    14. {
    15.     if (transform.position.x == 41)
    16.                     {
    17.                         gameGUI.missedTarget=true;
    18. //                      Destroy(this.gameObject);
    19.                     }  
    20. }
    21.  

    Any ideas? I have been trying to fix this all morning!! It's driving me nuts.
     
  2. ardo314

    ardo314

    Joined:
    Jul 7, 2012
    Posts:
    345
    Did you use DontDestroyOnLoad() on any of the objects if so, make sure your static variables get reset properly.
    And what exactly do you mean by stuck in a loop are both drawn simultaneously?
     
  3. wadek3

    wadek3

    Joined:
    Jan 16, 2013
    Posts:
    59
    I did not use DontDestroyOnLoad() and I have tried defining all of my static variables in both the Awake() and Start() functions- in all of my scripts for the game not just these. Neither works. Both are not drawn simultaneously but it goes notStartedTxture to loseTxture to notStartedTxture etc. to infinity. So the events that are supposed to trigger the lose texture are not triggering it but something is making missedTarget == true. I am going to restart my computer and unity and try again. this is driving me nuts.
     
  4. ardo314

    ardo314

    Joined:
    Jul 7, 2012
    Posts:
    345
    Hm maybe its that if-clause...i dont know how you assign the transforms position, but while comparing floats you normally dont check for equality.
    Use < / > / >= / <= instead.

    transform.position.x >= 41
     
  5. wadek3

    wadek3

    Joined:
    Jan 16, 2013
    Posts:
    59
    No that did not work but I do think that it is on that script. That script is attached to a game object that instantiates at the start of the game. I do not think that my Application.Load is erasing the update function on this script. That's my only theory. But even if I toggle on the Destroy(this.gameObject); in the script I still get the loop... and another problem occurs that the object does not re-instantiate?? I am at a total loss.
     
  6. wadek3

    wadek3

    Joined:
    Jan 16, 2013
    Posts:
    59
    Ok. So I am guessing that it is because I have these static variables. I am going to try and combine all of these scripts somehow... But shouldn't my static variables be reset the way that I have scripted this??? or have I done something wrong??
     
  7. wadek3

    wadek3

    Joined:
    Jan 16, 2013
    Posts:
    59
    OK. So I just checked my Awake() (above it is Start()) and it is changing the values on the reload. ThereforeI really think that the if (transform.position.x == 41) is not being reset on the reload of the game.
     
  8. wadek3

    wadek3

    Joined:
    Jan 16, 2013
    Posts:
    59
    This will be my last post on this until someone can help... But the level is definitely not reloading. I added a movie texture back in. When I press the GUI button it should reload the scene and the movie should start from the beginning. However, this is not happening. Therefore, I am assuming that my GUI button is not resetting the level. I have tried the following methods:

    Application.LoadLevel (Application.loadedLevel);
    Application.LoadLevel ("name");
    Application.LoadLevel (0);


    Nothing works!!!!!!!!!
     
  9. ardo314

    ardo314

    Joined:
    Jul 7, 2012
    Posts:
    345
    Just guessing into the blue...as i effectivly cant do anything else here. Do you instantiate the gameobject with the trigger functions at x-position 41?
     
  10. wadek3

    wadek3

    Joined:
    Jan 16, 2013
    Posts:
    59
    No, I instantiate it and then it moves to x-position 41 over 18 seconds. I have been doing quite a bit of trouble shooting with print() and I think that the problem is from the static variables. So now I am trying to figure out how to get rid of them and call on the variables with getcomponent.

    My script that gives the win/loss state is attached to cloneTargetA from the other script. Perhaps I can create a new boolean in my win loss state and call on that variable with the GUI script since Target is a gameobject within the component??? I have not done this before but I will post an update later today if I can figure it out.
     
  11. PawelBodytko

    PawelBodytko

    Joined:
    Feb 17, 2013
    Posts:
    35
    I do have problems when reloading levels also. But since I use Playmaker for my scripts, I blamed it on Playmaker rather then Unity... Things work fine the first time around, but once reloaded they start misbehaving...

    Are the problem related?
    http://hutonggames.com/playmakerforum/index.php?topic=3779.0