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 alexmurur · Aug 06, 2014 at 11:05 PM · instantiatematerialdiffuserandomize

Change Main Color to Predefined colors randomly

I am making a simple memory game that records colors and times the color is displayed. Previously, i asked a question here and built my code like this.. What i wanted to do is to display objects.

 #pragma strict
  
 public var OnScreen : Texture2D[] = new Texture2D[8];
 public var timeBetweenImages = 5.0 / 8.0;
  
 function Start () {
 // Shuffle Images
 var temp : Texture2D;
 for (var i = 0; i < OnScreen.Length-1; i++) {
 var j = Random.Range(i, OnScreen.Length);
 temp = OnScreen[i];
 OnScreen[i] = OnScreen[j];
 OnScreen[j] = temp;
 }
 DisplayImages();
 }
  
 function DisplayImages () {
 var i = 0;
 while(true) {
 renderer.material.mainTexture = OnScreen[i];
 i = (i + 1) % OnScreen.Length;
 yield WaitForSeconds(0.6);
  }
 }

I am suppposed to display all images once but the code is built for a loop i think..

I want to predefine 8 colors and change them as the image changes. This will not have any relativity to the object. In the end, the player is supposed to answer number of times the color has been displayed out of three options..

Currently, everything till the image display is fine.. However, I am getting this 'unmapped' look. How do i go about this whole thing?

alt text

s.png (85.9 kB)
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

1 Reply

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

Answer by robertbu · Aug 07, 2014 at 05:59 AM

Assuming you declare an initialize in the inspector an array of colors:

  public var colors : Color[];

You can set the renderer to a random color by:

  renderer.material.color = colors[Ransom.Range(0, colors.Length)];

Note if you only want to go through the images once, you can change the DisplayImages() function to:

 function DisplayImages () {
     for (var i = 0; i < OnScreen.Length; i++) {
         renderer.material.mainTexture = OnScreen[i];
         yield WaitForSeconds(0.6);
      }
 }
Comment
Add comment · Show 2 · 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 alexmurur · Aug 07, 2014 at 07:54 PM 0
Share

Works great.. Is the shuffling the same as texture shuffling? i am getting repeated colors..

avatar image robertbu · Aug 07, 2014 at 08:18 PM 0
Share

If you don't want repeat colors, then you would need to:

  • There should be the same number of colors as there are textures.

  • Shuffle the colors in a similar manner to how the textures were shuffled.

  • Display the colors using:


    renderer.material.mainTexture = colors[i];

Note there was an error in the code to only display images once that I just fixed.

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

2 People are following this question.

avatar image avatar image

Related Questions

Instantiate object with Emissive Material 2 Answers

How I can get an instace of a material, created in a project panel 1 Answer

Auto setting material type on import 1 Answer

Alpha Channel for selective transparency? 1 Answer

Instantiating objects with their own materials 0 Answers


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