Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by TheTrueHelix · Jun 12, 2016 at 07:02 AM · colorprefabsgeneration

Different color for each instance of a prefabs

So I'm currently trying to generate a world using a prefab with only a MeshRenderer on it. My goal is to generate a world and having each instances having a different color. I heard that you need to create a new material every time when u do so but it is not working right now, Any idea why ?

public class HexGeneration : MonoBehaviour {

 public GameObject platformMesh;
 public int height = 5;
 public int lenght = 5;

 // Use this for initialization
 void Start () {
     for (int i = 0; i < height; i++)
     {
         for (int j = 0; j < lenght; j++)
         {
             GameObject generatedPlatform = Instantiate(platformMesh, new Vector3(0,0,0), Quaternion.identity) as GameObject;
   
             Transform platformTransform = generatedPlatform.GetComponent<Transform>();
             ChangePosition(platformTransform, i, j);

             MeshRenderer platformRenderer = generatedPlatform.GetComponent<MeshRenderer>();
             ChangeMaterial(platformRenderer);
             
         }
     }
 }
 
 // Update is called once per frame
 void Update () {
 
 }

 void ChangeMaterial(MeshRenderer platformRenderer)
 {
     Material material = new Material(Shader.Find("Standard"));
     Color color = new Color(Random.Range(0, 1), Random.Range(0, 1), Random.Range(0, 1), 1);

     material.color = color;

     platformRenderer.material = material;
 }

 void ChangePosition(Transform platformTransform, float height, float lenght)
 {
     if (height % 2 == 0)
         platformTransform.Translate(lenght * 8.66f, 0, -height * 7.5f);
     else
     {
         if (height == 1) 
             platformTransform.Translate((lenght * 8.66f) + 4.33f, 0, -7.5f);
         else
             platformTransform.Translate((lenght * 8.66f) + 4.33f, 0, -height * 7.5f);

     }
 }

}

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 UsmanAbbasi · Jun 12, 2016 at 10:15 AM

You can change color like this:

  void ChangeMaterial(MeshRenderer platformRenderer)
  {
      Color color = new Color(Random.Range(0, 1), Random.Range(0, 1), Random.Range(0, 1), 1);
      platformRenderer.material.color = color;
  }

Accessing material property using "renderer.material" automatically creates new material for that object so you don't have to create it. You should know the difference between "material" and "sharedMaterial" property. This two links will help you understand this concept: https://docs.unity3d.com/ScriptReference/Renderer-material.html https://docs.unity3d.com/ScriptReference/Renderer-sharedMaterials.html

And by "it is not working", do you mean that color is not changing or are you having any errors?

Tip: If color is not changing then try to assign color like this just to make sure that issue is with color assigning and not somewhere else:

  platformRenderer.material.color = Color.red;


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 TheTrueHelix · Jun 12, 2016 at 05:13 PM 0
Share

Yeah it's good I have it working now, I understood what my problem was. You see, I was working with .FBX and when I was making my prefab I didn't realise that it came with a Light, A Camera, and a Cylinder thingy. So what I did was I created my mesh again, exported it in OBJ, and when I imported the mesh I still put the mesh as default but as a child of something. So I took default and made it my prefab. $$anonymous$$y error was that I was changing the color of the parent and not the cylinder thingy when I was working with the fbx.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Is there a way to control when lighting is generated? 0 Answers

generating hallways using prefabs based on difficulty 1 Answer

Prefab block based level generation | Generating a level from prefab blocks 0 Answers

My public variables are lost when I convert my game object into a prefab 1 Answer

I need help adding an initial upwards velocity to some cloned prefabs. 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