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.
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.
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.");
}
}
Your answer
Follow this Question
Related Questions
Changing a texture as a float or int drops 2 Answers
public vs private array in classes 0 Answers
Soundarray for player is not working properly 1 Answer
How do I copy UV variables? 2 Answers