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

Global Variable issue - Please help! should be simple.

Discussion in 'Editor & General Support' started by Entity3Limited, Jan 20, 2012.

  1. Entity3Limited

    Entity3Limited

    Joined:
    Jan 10, 2012
    Posts:
    21
    Hi,

    I am having some issues with a global variable. Please see if you can solve this for me.

    I have the global var set up as below in a script called GlobalValues (this is not attached to a gameobject)

    In a separate script attached to a GUI texture I check this variable and if is set to '3' then I change the image of the GUI,as below

    Now, My problem seems to lie in my next script. This script below is attached to another GUI texture. On holding down the mouse key it plays an animation of a gameobject, and reverses it if the button is lifted.
    What I want to do is change the global value to '3' when the animation is playing.
    So I use the code
    to change it to a '3'.
    If this is lose in the script (not in a function) it works, but under an if statement or as it is below the request to change the value to a '3' seems to be ignored.

    I am sure this is simple to a trained eye.

    I hope you can help, Thanks
     
  2. Moonjump

    Moonjump

    Joined:
    Apr 15, 2010
    Posts:
    2,571
    The if statement needs to be inside function Update.
     
  3. Entity3Limited

    Entity3Limited

    Joined:
    Jan 10, 2012
    Posts:
    21
    I tried your suggestion but the result was the same.
    Value is not changed to 3.

    Any other ides?
     
  4. Moonjump

    Moonjump

    Joined:
    Apr 15, 2010
    Posts:
    2,571
    Try the following. I've also put some other lines inside functions that look as if they need to be there. The other thing I have done is add a print statement. If that appears, then your if statement is working. You can safely remove the print line once you know that.

    Code (csharp):
    1. var theMainCharacter: GameObject;
    2.  
    3. private var gui : GUITexture;
    4.  
    5.  
    6. function Start(){
    7.  
    8. gui = GetComponent( GUITexture );
    9. theMainCharacter = GameObject.FindWithTag("Man");
    10. theMainCharacter.animation.wrapMode = WrapMode.Once;
    11. }
    12.  
    13. function Update () {
    14.  
    15. if (theMainCharacter.animation["Anim1"].enabled == true)
    16. {
    17. GlobalValues.buttonheld=3;
    18. print ("the if statement is working");
    19. }
    20.  
    21. if (Input.GetMouseButton(0))
    22. {
    23.  
    24. if(theMainCharacter.animation["Anim1"].time<0)
    25. theMainCharacter.animation["Anim1"].time=0; // onEnter
    26.  
    27. theMainCharacter.animation["Anim1"].speed = 1;
    28. theMainCharacter.animation.CrossFade("Anim1"); 
    29. }
    30. else
    31. {
    32.  
    33. if(theMainCharacter.animation["Anim1"].normalizedTime>1)
    34.  
    35. theMainCharacter.animation["Anim1"].normalizedTime=1; // onExit
    36.  
    37. theMainCharacter.animation["Anim1"].speed = -1;
    38. theMainCharacter.animation.CrossFade("Anim1"); 
    39. }
    40.  
    41.  
    42. }
     
  5. Entity3Limited

    Entity3Limited

    Joined:
    Jan 10, 2012
    Posts:
    21
    Thanks for your input.
    The print ("the if statement is working"); shows but even without clicking the button.
    It also seem to repeat over and over, yet still buttonheld will not set to 3.

    PLEASE!!!! I would love to solve this