Search Unity

[SOLVED]Changing GUI Text dynamically, why is it so hard?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Chowderman, Sep 21, 2009.

  1. Chowderman

    Chowderman

    Joined:
    May 21, 2009
    Posts:
    92
    So, I have a character scripted to dance when the iPhone screen is tapped. He cycles through 5 different dances as the player taps on the screen, with a series of if/else if statements. What I want to do is have a GUI that tells the player the name of the current dance the character is doing. I've tried making a variable and having it changed in the same if/else if statement that changes the dance, but that variable isn't changing the GUI at all, since I can only make GUI.Label changes within the OnGUI function.

    What am I doing wrong?
     
  2. jmunozar

    jmunozar

    Joined:
    Jun 23, 2008
    Posts:
    1,091
    Hello,

    Can you please paste code so we can help you out? :)
     
  3. Chowderman

    Chowderman

    Joined:
    May 21, 2009
    Posts:
    92
    which code? I was just trying single lines of things but every time I'd add anything into my else if statements it would yell at me that GUI things could only be called in the OnGUI function
     
  4. jmunozar

    jmunozar

    Joined:
    Jun 23, 2008
    Posts:
    1,091
    Hello,

    All your GUI functions have to be inside the OnGUI function :), what you are telling me is that they are not inside the OnGUI function.

    you might be wondering, how are you going to connect the OnGUI code with your animation.
    well.. its really simple, use global vars as flags, and trigger those flags from the OnGUI and use them on your update/fixed Update :)

    hope it helps :D!
     
  5. Chowderman

    Chowderman

    Joined:
    May 21, 2009
    Posts:
    92
    where should I declare my global vars so that the onGUI code can see them? Right now I have code attached to my character to make him move when tapped that looks like this:

    Code (csharp):
    1. function Update () {
    2. var hit : RaycastHit;
    3.          for (var touch : iPhoneTouch in iPhoneInput.touches)  
    4. {
    5.    if (touch.phase == iPhoneTouchPhase.Began)
    6.    {
    7.           var ray = Camera.main.ScreenPointToRay (touch.position);
    8.                 if (Physics.Raycast (ray, hit, 100)  hit.collider.tag == "Player")
    9.       {
    10.         if(animation.IsPlaying("idle"))
    11.         {
    12.             animation.CrossFade("Dance1", 0.5);
    13.         }
    14.         else if(animation.IsPlaying("Dance1"))
    15.         {
    16.             animation.CrossFade("Dance2", 0.5);
    17.         }
    18.         else if(animation.IsPlaying("Dance2"))
    19.         {
    20.             animation.CrossFade("Dance3", 0.5);
    21.         }
    22.         else if(animation.IsPlaying("Dance3"))
    23.         {
    24.             animation.CrossFade("Dance4", 0.5);
    25.         }
    26.         else if(animation.IsPlaying("Dance4"))
    27.         {
    28.             animation.CrossFade("Dance5", 0.5);
    29.         }
    30.         else
    31.         {
    32.             animation.CrossFade("idle", 0.5);
    33.         }
    34.         }
    35.         }
    36.         }
    37. }
    so should I create a new script and attach it to the camera with all the OnGUI functions and declare my public vars there? How do I get the two scripts to talk to one another? My Dance script (above) is different from my GUI script, attached to the camera.
     
  6. Timmer

    Timmer

    Joined:
    Jul 28, 2008
    Posts:
    330
    I am not familiar with the Animation object but looking at the script reference, I didn't see a "get current state name in play" method which seems like it would be nice for you.

    So in the meantime you could do something like this:
    Code (csharp):
    1.  
    2. var animationName : string;
    3.  
    4. function Update ()
    5. {
    6.    var hit : RaycastHit;
    7.    for (var touch : iPhoneTouch in iPhoneInput.touches)
    8.    {
    9.        if (touch.phase == iPhoneTouchPhase.Began)
    10.        {
    11.           var ray = Camera.main.ScreenPointToRay (touch.position);
    12.           if (Physics.Raycast (ray, hit, 100)  hit.collider.tag == "Player")
    13.           {
    14.              // Get the next animation to play
    15.              if(animation.IsPlaying("idle"))
    16.              {
    17.                 animationName = "Dance1";
    18.              }
    19.              else if(animation.IsPlaying("Dance1"))
    20.              {
    21.                 animationName = "Dance2";
    22.              }
    23.              // ...
    24.              else
    25.              {
    26.                 animationName = "idle";
    27.              }
    28.              // Cross fade to the animation
    29.              animation.CrossFade(animationName, 0.5);
    30.          }
    31.       }
    32.    }
    33. }
    34.  
    35. function OnGUI()
    36. {
    37.     GUI.Label(Rect(0, 0, 200, 25), animationName);
    38. }
     
  7. Chowderman

    Chowderman

    Joined:
    May 21, 2009
    Posts:
    92
    works perfectly. Don't know why I couldn't figure out such a simple solution. I attempted something similar, but I didn't have the OnGUI function in the correct place. Thanks!
     
  8. Timmer

    Timmer

    Joined:
    Jul 28, 2008
    Posts:
    330
    No worries, we all make blind mistakes or just sometimes takes someone from a fresh point of view. Glad to help!