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 Valkarth · Apr 19, 2017 at 07:29 AM · colorarrayscolor changearray of gameobjectselements

Assigning a different material to different objects

Hey,

I want to be able to change the material of a gameObject dependent on the value of starclass. I have 7 materials which have been placed into the Inspection slots for element[0-6].

I've established that I have to essentially create a new material of size 1 and re-assign the the appropriate element back into the inspector but I just can't seem to do it. Any ideas?

Code below....

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class StarInfo : MonoBehaviour {

 public float starID;
 public string starclass;
 private string starcolor;
 public int startemp;
 private float randomintfloat;
 public GameObject star;
 public Material[] materials;
 public Renderer rend;


 public void Start()
 {
     rend = gameObject.GetComponent<Renderer> ();
     rend.enabled = true;

     Invoke("Classify", 0f);
 }

 public void Classify()
 {
     switch (starclass) 
     {
     case "O":
         Debug.Log ("Should change color to O");
         rend.materials = new Material[1];
         rend.material = classmaterial[1];
         //star.GetComponent<Renderer>().material.color = new Color(155, 176, 255);
         break;
     case "B":
         Debug.Log ("Should change color to B");
         rend.materials = new Material[1];
         rend.material = classmaterial[2];
         //star.GetComponent<Renderer>().material.color = new Color(170, 191, 255);
         break;

     case "A":
         Debug.Log ("Should change color to A");
         rend.materials = new Material[1];
         rend.material = classmaterial[3];
         //star.GetComponent<Renderer>().material.color = new Color(202, 215, 255);
         break;

     case "F":
         Debug.Log ("Should change color to F");
         rend.materials = new Material[1];
         rend.material = classmaterial[4];
         //star.GetComponent<Renderer>().material.color = new Color(248, 247, 255);
         break;

     case "G":
         Debug.Log ("Should change color to G");
         rend.materials = new Material[1];
         rend.material = classmaterial[5];
         //star.GetComponent<Renderer>().material.color = new Color(255, 244, 234);
         break;

     case "K":
         Debug.Log ("Should change color to K");
         rend.materials = new Material[1];
         rend.material = classmaterial[6];
         //star.GetComponent<Renderer>().material.color = new Color(255, 210, 161);
         break;

     case "M":
         Debug.Log ("Should change color to M");
         rend.materials = new Material[1];
         rend.material = classmaterial[6];

         //star.GetComponent<Renderer> ().material.color = new Color (255, 204, 111);
         break;

     }

     Debug.Log ("The clones have been classified and colored.");
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by cleonmocika · Apr 19, 2017 at 08:26 AM

Instead of rend.material = classmaterial[index], try using rend.sharedMaterial = classmaterial[index]. You also don't need the line that says rend.materials = new Material[1], there is no need to create a new material cause you're just using the ones you already created in the editor.

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 Valkarth · Apr 19, 2017 at 12:27 PM

@cleonmocika,

Thanks for the response, I tried what you said but nothing changed. I'm still trying to find a solution.

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

99 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 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 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 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 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 make RANDOM COLOR on shader 1 Answer

OnGUI() Text Color through c# script 1 Answer

Why Color.Lerp or Color32.Lerp doesnt work properly? 1 Answer

how to change object's B color when object A is in range? 2D 1 Answer

How to only access one object in array if it's index is 1 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