x


triggering random animations with gui

I have a GUI button that triggers an animation. But I want it to trigger animations randomly every time it is pressed. So that the game object which has 10 animations ( spin1,spin2,..)are played randomly each time the trigger is pressed. BUT due to my inexperience I dont really understand arrays. What I have at the moment is simply this

function OnGUI() {

if(GUI.Button(Rect (250,0,600,1000), " ")){

    animation.CrossFade("spin2");

} }

but i have so far changed it to something like this

function OnGUI() {
if(GUI.Button(Rect(250,0,600,1000),"test",customButton)){
    animation.CrossFade(Random.Range(0,animation.elementCount));

}
}

Thanks for any help.

more ▼

asked Sep 29 '10 at 08:39 AM

richardzzzarnold gravatar image

richardzzzarnold
72 16 20 28

mark correct answers with a tick

Sep 29 '10 at 05:23 PM spinaljack
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You need to store the animation list in an array or you can store the name of the animation in a String array, which ever you prefer

e.g.

var animName : String[];

function OnGUI() {
   if(GUI.Button(Rect(250,0,600,1000),"test",customButton)){
       animation.CrossFade(animName[Random.Range(0,animName.length)]);
   }
}

You set the size and fill the array with names in the inspector. You return the contents array by typing the name of the array followed by square brackets with a number inside. Remember that arrays start from 0 and not 1.

e.g.

print(arrayName[1]);

Array docs:

http://unity3d.com/support/documentation/ScriptReference/Array.html

Typical array functions:

http://msdn.microsoft.com/en-us/library/system.collections.arraylist_members(VS.80).aspx

more ▼

answered Sep 29 '10 at 12:26 PM

spinaljack gravatar image

spinaljack
9.1k 18 31 91

Thank you ! I have it working now!

Sep 29 '10 at 04:12 PM richardzzzarnold
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3807
x3698
x986
x575
x106

asked: Sep 29 '10 at 08:39 AM

Seen: 1544 times

Last Updated: Sep 29 '10 at 12:20 PM