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
1
Question by RandomUser123 · Aug 24, 2014 at 09:32 AM · materialrandomgameobjects

Assign a random material to objects

I have 5 objects in my scene and 5 materials, what I would like is for every time the scene starts, the objects will have a random material based on the 5 materials. I would only like each material to be used once. I have this so far:

     public Material[] randomMaterials;
 
     public GameObject[] coloredWalls;
 
     void Start () 
     { 
         foreach(Material materials in randomMaterials)
         {
             //randomMaterial[materials] = Random.Range (0, 5);
             foreach(GameObject wall in coloredWalls)
             {
                 wall.renderer.material = materials;
             }
         }
     }

I have it assigning a material at the moment, but as it iterates through the foreach loop without assigning a random value, all of the objects are always the last material in the array.

I tried giving the material a random value using the Random.Range, but unity doesn't seem to like that. If anyone could give me a hand I'd appreciate it.

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
5
Best Answer

Answer by zharik86 · Aug 24, 2014 at 10:59 AM

As far as I understood, you apply an random material to each object in case of initialization. The code sample is lower:

  public Material[] randomMaterials;
  public GameObject[] coloredWalls;

  void Start ()  {
   foreach(GameObject wall in coloredWalls) { //see all objects
    //Assign random material to object
    wall.renderer.material = randomMaterials[Random.Range(0, randomMaterials.Length)];
   }
  }

I hope that it will help you.

Comment
Add comment · Show 4 · 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 RandomUser123 · Aug 24, 2014 at 11:08 AM 0
Share

Hi @zharik86, yes that works much better, thank you :) Just a small thing, is there a way using this method that I could use each material only once?

At the moment, when I play, sometimes, 2 or more objects can have the same material

avatar image zharik86 · Aug 24, 2014 at 11:33 AM 1
Share

@RandomUser123 I understood, materials will have no recurrence. I will rewrite a script of the response with adding of new comments:

  public $$anonymous$$aterial[] random$$anonymous$$aterials;
  public GameObject[] coloredWalls;
 
  void Start ()  {
   //Simple way: create int array for indicate assign material
   int[] ind$$anonymous$$at = new int[random$$anonymous$$aterials.Length];
   //Just in case we initialize an array
   for(int i = 0; i < random$$anonymous$$aterials.Length; i++) {
    ind$$anonymous$$at[i] = 0; //not assign material
   }
   foreach(GameObject wall in coloredWalls) { //see all objects
    //Assign random material to object
    int num = Random.Range(0, random$$anonymous$$aterials.Length);
    //already apply, find next
    while(ind$$anonymous$$at[num] != 0) {
     num++;
     if (num >= random$$anonymous$$aterials.Length) {
      num = 0;
     }
    }
    ind$$anonymous$$at[num] = 1;
    wall.renderer.material = random$$anonymous$$aterials[num];
   }
  }
avatar image RandomUser123 · Aug 24, 2014 at 11:54 AM 0
Share

Wow, this works great, I would have never gotten this ha :( maybe one day :)

Anyway, thank you so much for your time, I appreciate it :)

avatar image zharik86 · Aug 24, 2014 at 06:43 PM 0
Share

@RandomUser123 At your service, but you remember that the count of materials shall be more or equal count of objects, differently there will be an infinite cycle.

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Randomly Add a Gameobject from a set of GameObjects to another set of Gameobjects 1 Answer

How to Spawn multiple game object one by one in random order 1 Answer

Random Material in specific multiple materials element 1 Answer

Count Objects with specific colour 1 Answer

Changing / Adding Material over Real-World Time 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