Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by cacysunlee · Feb 10, 2011 at 08:23 AM · randomcolorhoveronmouseenter

OnMouseEnter random hover color

i would like to know how to set a random color for a mouse hover here is my first try with random, is my array wrong?

// old : renderer.material.color = Color.gray;

 var arrayHovercolor = new Array(renderer.material.color);
     arrayHovercolor.Push (Color.gray);  
     arrayHovercolor.Push (Color.green);
     arrayHovercolor.Push (Color.blue);
     arrayHovercolor.Push (Color.red);
     arrayHovercolor.Push (Color.white);
     arrayHovercolor.Push (Color.black);
     arrayHovercolor.length = 6;         

         var randomColor = renderer.material.color;
         randomColor = arrayHovercolor(Random.Range(0,arrayHovercolor.length));
         print("i hover here"); //debug

Comment
Add comment
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

4 Replies

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

Answer by Mike 3 · Feb 10, 2011 at 01:27 PM

These should fix it:

//you used a colour here - it only takes an int for size
var arrayHovercolor = new Array();

and

//square brackets for accessing array items randomColor = arrayHovercolor[Random.Range(0,arrayHovercolor.length)];

//don't forget to set the material's colour to the picked color renderer.material.color = randomColor;

You also don't need this line, so add var to the randomColor = line above:

var randomColor = renderer.material.color;
Comment
Add comment · 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
0

Answer by Jesse Anders · Feb 10, 2011 at 10:15 AM

Try:

renderer.material.color =
    arrayHovercolor(Random.Range(0,arrayHovercolor.length));

(Assuming UnityScript doesn't differ from C# in this respect, Color is a value type rather than a reference type, so you can't acquire a reference to it as you appear to be trying to do currently.)

Comment
Add comment · Show 3 · 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 cacysunlee · Feb 10, 2011 at 10:41 AM 0
Share

thank you, i try this in my java-script, but unity don't compile and give me this : "It is not possible to invoke an expression of type 'Array'."

avatar image Jesse Anders · Feb 10, 2011 at 11:26 AM 1
Share

You most likely need square brackets there rather than parentheses, i.e. arrayHovercolor[...].

avatar image cacysunlee · Feb 10, 2011 at 11:40 AM 0
Share

thank you, i try this, but unity don't compile again : Cannot convert 'UnityEngine.Color' to 'int'.

avatar image
0

Answer by cacysunlee · Feb 10, 2011 at 01:53 PM

thank you very much now i solved it :

function OnMouseEnter(){ 
        var arrayHovercolor = new Array();
            arrayHovercolor.Push (Color.gray);  
            arrayHovercolor.Push (Color.green);
            arrayHovercolor.Push (Color.blue);
            arrayHovercolor.Push (Color.red);
            arrayHovercolor.Push (Color.white);
            arrayHovercolor.Push (Color.black);
                    renderer.material.color = arrayHovercolor[Random.Range(0,arrayHovercolor.length-1)];
                print("i hover here"); //debug
}

Comment
Add comment · 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
0

Answer by Dess · Feb 23, 2011 at 02:37 PM

how can I integrate this code in my HTML?

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Change text color on GUI.Label 1 Answer

Random colors for particles? 0 Answers

C# Randomize Background.Color Issues 1 Answer

random Color. Glitch?!? 1 Answer

Random objects don't overlap 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