• 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 01:35 PM · c#arrayrenderingmaterialclones

Applying Material to individual clones

Hi,

I'm having trouble changing the material of a renderer given a certain value. The starclass determines the material of the renderer. I have created 7 materials and placed them into a folder each being of different colours and when I manually select them onto a star it works. My program in another script instantiates 20 clones of my prefab and runs the script below on each individual clone.

alt text

In my prefab, I have selected the elements of my material and given it size 7. Seen above.

I believe these are the possible solutions: - Make all 7 elements of the same material to make it appear as if it is one solid colour. - Destroy the renderer, then create one and apply the appropriate material onto it - Reduce the size of the array down to 1 and make the first element respective to the starclass.

Below is the inspector of a clone made.

alt text

I've been stuck on this for about 8 hours now and i'm bloody frustrated. Any comments are deeply appreciated.

My code below:

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

 public class StarInfo : MonoBehaviour {

 //Star Properties
 public float starID;
 public string starclass;
 public int startemp;

 //Star Material and Color
 private float randomintfloat;
 public Material[] materials;
 public Renderer rend;

 //Optional:
 //public GameObject star;
 //private string starcolor;

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

     Invoke("Classify", 0f);
 }

 public void Classify()
 {
     //star = gameObject;
     // The starID is simply a float which has its value determined by a random number in the range of 1001 to 9999.
     // This is purely for design purposes.
     starID = Random.Range (1001, 9999);

     // Initialising the star class.
     starclass = "";

     // Generating a random integer between 0 and 6 which represent the 7 possible star types (soon to be assigned).

     randomintfloat = Random.Range (0, 6);
     int randomint = (int)Mathf.Round (randomintfloat);
     Debug.Log ("The value of randonint after it has been rounded is " + randomint);

     // Casewhere randomint is...
     switch (randomint) 
     {
     case 0:
         starclass = "O";
         // The startemp is in the range between the commonly found temperatures. Temperatures seen on the page below.
         // https://en.wikipedia.org/wiki/Stellar_classification
         startemp = Random.Range (30000, 50000);
         break;
     case 1:
         starclass = "B";
         startemp = Random.Range (10000, 29999);
         break;
     case 2:
         starclass = "A";
         startemp = Random.Range (7500, 9999);
         break;
     case 3:
         starclass = "F";
         startemp = Random.Range (6000, 7499);
         break;
     case 4:
         starclass = "G";
         startemp = Random.Range (5200, 5999);
         break;
     case 5:
         starclass = "K";
         startemp = Random.Range (3700, 5199);
         break;
     case 6:
         starclass = "M";
         startemp = Random.Range (2400, 3699);
         break;
     }

     Debug.Log("The starclass is now " + starclass);
     Debug.Log ("The temperature of the star is now " + startemp);

     // In the case where starclass is O|B|A|F|G|K|M , change the starcolor according to the class
     // http://www.vendian.org/mncharity/dir3/starcolor/

     switch (starclass) 
     {
     case "O":
         Debug.Log ("Should change color to O");
         rend.sharedMaterial = materials[0];
         break;
     case "B":
         Debug.Log ("Should change color to B");
         rend.sharedMaterial = materials[1];
         break;
     case "A":
         Debug.Log ("Should change color to A");
         rend.sharedMaterial = materials[2];
         break;
     case "F":
         Debug.Log ("Should change color to F");
         rend.sharedMaterial = materials[3];
         break;
     case "G":
         Debug.Log ("Should change color to G");
         rend.sharedMaterial = materials[4];
         break;
     case "K":
         Debug.Log ("Should change color to K");
         rend.sharedMaterial = materials[5];
         break;
     case "M":
         Debug.Log ("Should change color to M");
         rend.sharedMaterial = materials[6];
         break;
     }

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

}

screen-shot-2017-04-19-at-112635-pm.png (52.7 kB)
screen-shot-2017-04-19-at-113225-pm.png (52.2 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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

347 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 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 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 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

Changing a texture as a float or int drops 2 Answers

How to get variable by name(string typed in inspector) in array with index from another script? 0 Answers

IndexOutOfRangeException error when getting transform of array object 0 Answers

Shader not working properly in a build 0 Answers

Move enemy to a specific movepoint with an array (C#) 1 Answer


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