- Home /
GUI Button Animation Cue
Is it possible to cue two separate objects animations from one GUI Button? For example, I have the following script attached to my antenna object which triggers an animation that I created in Unity. It works great, but I want to move the camera at the same time. If I attach the same script to the camera, I of course get two buttons on top of one another. Can I somehow reference the camera's animation to play from the script attached to my antenna object? Or is there a better method to do this that I'm not even considering?
Here's the code:
function OnGUI()
{
if( GUI.RepeatButton( Rect( 25, 25, 130, 30 ), "Deplay Anetnna" ) )
{
animation.Play();
}
}
Answer by Joshua · Jul 14, 2011 at 04:02 PM
animation.Play(); is actually 'short' for gameObject.animation.Play(), where gameObject is the gameObject your script is currently attached to.
If you want to reference a component of an other GameObject, there are tons of ways you can establish this connection.
You could, at the top of your script have a variable of the Type GameObject or even Animation and drag-and-drop through the inspector:
var myCamera : GameObject;
myCamera.animation.Play()
or
var myAnimation : Animation;
myAnimation.Play();
you can also establish this reference dynamically, by using gameObject.Find() for instance. This is often not a good idea however, because it'll do a linear search through all of your gameObjects, which takes relatively long.
If you want to reference your camera however, and it's tagged MainCamera, as it is by default, you're in luck. You can reference it by using Camera.main, so it would look like this:
Camera.main.animation.Play();
But this of course only works for a MainCamera, the solutions I mentioned above work for every other type of GameObject as well.
Thanks Joshua, the Camera.main.animation.Play(); method worked perfectly. Appreciate the help!