playing random animation

How I can set so this button will play a random animation attached to my model?
I have 20 animation on the 3d model but I want to play one of the 20 animation randomly each time i press the button.

var hand : GameObject; 

function OnGUI(){
		if (GUI.Button (Rect (105, 457, 100, 22), "Quiz")) {  
   
	hand.animation.Play("J");
 		
}

i use this line to var allmotions : AnimationClip[];

make an array from all my animation on the object, now how do I play
the a random animation for this array on my object?

Did you just try something like?

animation.Play(allmotions[Random.Range(0, allmotions.Length)]);

i did early on a test with
animation.Play(allmotions[1]) and i justed your code line. I get the same

error:

no appropriate version of UnityEngine.Animation.Play for argument list (UnityEngine.AnimationClip)’ was found

lesfundi

That is what I get for paying partial attention and not really thinking through my response.

You want to use Animation.AddClip() and then call Animation.Play() on them based on the names that you add.

i don’t understand the addclip. I imported my .fbx files and did the split of the animation on the object. I see the different animation clips on my object.

i created first array and manual dropped my animation in their.

var allmotions : AnimationClip[ ]; → so that doesn’t work than?

thanks for all help! :slight_smile:

I meant something like this (again, quick and untested) – I don’t know why you didn’t add the clips to the animation directly though and call them there, but this uses the allmotions array that you had (I guess you want it that way for some other reason?)

var allmotions : AnimationClip[];
function Start () {
	for (var i = 0; i < allmotions.Length; i++)
		animation.AddClip(allmotions[i], "random"+i);
	
}

function OnGUI(){ 
      if (GUI.Button (Rect (105, 457, 100, 22), "Quiz")) {  
	  var random = Random.Range(0, allmotions.Length);
	  animation.Play("random"+random);    
      }
}

:slight_smile: nice, thank you so much :slight_smile:

The Animation.Play function only recognizes strings (not AnimationClips for some reason).

The only way I’ve found that works is to change this:

var allmotions : AnimationClip[];

Into this:

var allmotions : String[];

And then type the names of the animations into the array.

I’m sure there’s a way to convert a value into a String within the script, but I have yet to find the function. Someone what to enlighten us? :slight_smile:

Here’s the rest of the code:

var allmotions : String[];
var hand : GameObject; 

function OnGUI()
{
	var randomAnim = allmotions[Random.Range (0, whatever)];
	
	if (GUI.Button (Rect (105, 457, 100, 22), "Quiz"))
	{
		hand.animation.Play (randomAnim);
	}
}

Edit: After reading Charles post, I remembered THIS little gem (I changed the hand.animation.Play Line):

var allmotions : AnimationClip[];
var hand : GameObject; 

function OnGUI()
{
	var randomAnim = allmotions[Random.Range (0, whatever)];
	
	if (GUI.Button (Rect (105, 457, 100, 22), "Quiz"))
	{
		hand.animation.Play ("" + randomAnim);
	}
}

“” + Value means that the variable will convert it’s value to a string. I can’t believe I forgot about it! :smile:

this is very interesting!

I have a need to play random animations also. I tried each code sample in this post without success. If I hard code the animation name in the animation.Play call that animation will play. I cannot get the random substitution working.

Thanks,Steve

What is going wrong? Does the same animation play each time, do they play in non-random order…? Maybe you can post something you’ve tried and explain what goes wrong with it.

If I hard code the name of the animation in the animation.Play call then that specific animation plays when the user touches the screen.

example:

animation.Play(“idle_breath”); plays a specific animation of the puppet breathing

but:

The following displays the breathing puppet but nothing happens when I touch the screen. If I hardcode any of the animations into the animation.Play call in the Update function then that specific animation will play when I touch the screen.

var puppet: GameObject;

var actions : String[ ];

actions[0] = “idle_breath”;
actions[1] = “face_punch”;
actions[2] = “slap_left”;
actions[3] = “belly_punch”;
actions[4] = “slap_right”;
actions[5] = “squeeze_body”;
actions[6] = “squeeze_head”;

function Update () {

var random = actions[Random.Range (0, 5)];

if (iPhoneInput.touchCount ==1)
{
//var random = actions[Random.Range (0, 5)];
animation.Play(“” + random);
}
}

The call to the Random function in the above code snippet is not commented out in my running example. It was commented out in this specific version of the script because I was testing.

Thanks,Steve

Try using “iPhoneInput.touchCount > 0” as the condition. Also, the second parameter to Random.Range should be the number of items in the array (ie, one greater than the highest index).

Thanks for the tips. The entire script is included below. I still get the same behavior. I’ve tried each of the calls to Random with the same result. If I hard code an animation name in the “touch if” block then that animation will play. Any other thoughts you might have after seeing this script are really appreciated.

Thanks,Steve

var puppet: GameObject;

var actions : String[ ];

actions[0] = “idle_breath”;
actions[1] = “face_punch”;
actions[2] = “slap_left”;
actions[3] = “belly_punch”;
actions[4] = “slap_right”;
actions[5] = “squeeze_body”;
actions[6] = “squeeze_head”;

function Start () {

//Set all animations to play once
animation.wrapMode = WrapMode.Once;
//except breathing
animation[“idle_breath”].wrapMode = WrapMode.Loop;

//stop any other animations
animation.Stop();
animation.Play(“idle_breath”);

}

function Update () {

var random = actions[Random.Range (0, 8)];

if (iPhoneInput.touchCount > 0)
{
//var random = actions[Random.Range (0, 8)];
animation.Play(“” + random);
}
}