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

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?

Hello,

Can you please paste code so we can help you out? :slight_smile:

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

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 :slight_smile:

hope it helps :smile:!

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:

function Update () { 
var hit : RaycastHit; 
         for (var touch : iPhoneTouch in iPhoneInput.touches)  
{ 
   if (touch.phase == iPhoneTouchPhase.Began) 
   { 
          var ray = Camera.main.ScreenPointToRay (touch.position); 
                if (Physics.Raycast (ray, hit, 100)  hit.collider.tag == "Player") 
      { 
      	if(animation.IsPlaying("idle"))
      	{
      		animation.CrossFade("Dance1", 0.5); 
      	}
      	else if(animation.IsPlaying("Dance1"))
      	{
      		animation.CrossFade("Dance2", 0.5);
      	}
      	else if(animation.IsPlaying("Dance2"))
      	{
      		animation.CrossFade("Dance3", 0.5);
      	}
      	else if(animation.IsPlaying("Dance3"))
      	{
      		animation.CrossFade("Dance4", 0.5);
      	}
      	else if(animation.IsPlaying("Dance4"))
      	{
      		animation.CrossFade("Dance5", 0.5);
      	}
      	else
      	{
      		animation.CrossFade("idle", 0.5);
      	}
      	}
		}
      	}
}

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.

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:

var animationName : string;

function Update () 
{
   var hit : RaycastHit;
   for (var touch : iPhoneTouch in iPhoneInput.touches) 
   {
       if (touch.phase == iPhoneTouchPhase.Began)
       {
          var ray = Camera.main.ScreenPointToRay (touch.position);
          if (Physics.Raycast (ray, hit, 100)  hit.collider.tag == "Player")
          {
             // Get the next animation to play
             if(animation.IsPlaying("idle"))
             {
                animationName = "Dance1";
             }
             else if(animation.IsPlaying("Dance1"))
             {
                animationName = "Dance2";
             }
             // ...
             else
             {
                animationName = "idle";
             }
             // Cross fade to the animation
             animation.CrossFade(animationName, 0.5); 
         }
      }
   }
}

function OnGUI()
{
    GUI.Label(Rect(0, 0, 200, 25), animationName);
}

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!

No worries, we all make blind mistakes or just sometimes takes someone from a fresh point of view. Glad to help!