• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by richardzzzarnold · Sep 29, 2010 at 08:39 AM · animationguitriggerrandomrange

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image spinaljack · Sep 29, 2010 at 05:23 PM 0
Share

mark correct answers with a tick

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by spinaljack · Sep 29, 2010 at 12:26 PM

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image richardzzzarnold · Sep 29, 2010 at 04:12 PM 0
Share

Thank you ! I have it working now!

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Path Trigger Animation 1 Answer

How do i make a text box appear on screen after a chest has been opened? 3 Answers

Adjusting ObjectLabel to activate animations 1 Answer

random range for gui button 1 Answer

How to get random GUI to appear, on entering a trigger. (PIC INCLUDED) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges