Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Oragoss · Jul 18, 2015 at 11:26 AM · arraymaterialloop

Has anyone ever used code to change the materials on an instantiated game object?

Hello! So I'm trying to change the material on a quad to a random picture whenever I load up the game. I'm using a for loop to instantiate the quad's so it would be great if I could make their materials random.

Yes I read the API and it wasn't helping me with my specific circumstance which is why I was hoping someone here could point me in the right direction.

 public Material[] mats;
 void levelCreate()
     {
         Material mat = mats[Random.Range(0, mats.Length)];
         if (easy == true)
         {
             GetComponent<Renderer>().material = mat;
             for (int i = 0; i < 5; i++) //makes the columns in the 5 by 5 grid
             {
                 for (int j = 0; j < 6; j++) //makes the rows in a 5 by 5 grid
                 {
                     Instantiate(prefab, new Vector3(j * 1.5f - 3, i * -1.5f + 3, 0), transform.rotation);
                     prefab.name = count.ToString();
                     count++;
                     gameStart = false;
                 }
             }
         }
     }

Oh and I read the API from http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html http://docs.unity3d.com/ScriptReference/Renderer-material.html and http://docs.unity3d.com/ScriptReference/Random.Range.html

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
1

Answer by DoTA_KAMIKADzE · Jul 18, 2015 at 11:29 AM

Something like that:

     public Material[] mats;
     void levelCreate()
     {
         Material mat = mats[Random.Range(0, mats.Length)];
         if (easy == true)
         {
             for (int i = 0; i < 5; i++) //makes the columns in the 5 by 5 grid
             {
                 for (int j = 0; j < 5; j++) //makes the rows in a 5 by 5 grid
                 {
                     GameObject go = Instantiate(prefab, new Vector3(j * 1.5f - 3, i * -1.5f + 3, 0), transform.rotation) as GameObject;
                     go.name = count.ToString();
                     //if you have only 1 material:
                     go.GetComponent<Renderer>().material = mat; //if you have multiple materials then loop through materials[X]
                     count++;
                     gameStart = false;
                 }
             }
         }
     }

I've removed your line#7, get it back if it was intended, also j < 6 => j < 5.

The main part of the answer is obviously Instantiation (line #11 and #14 in my example).

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do i get texture from GUI box? 1 Answer

Array loop first element goes last? 1 Answer

Trouble with for loop out of range. Simple? Maybe. 1 Answer

Trouble checking against array objects 0 Answers

Material doesn't have a color property '_Color' 4 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