public void CreateGem()
{
color = GemMat[Random.Range(0,GemMat.Length)];
Material m= Resources.Load("Materials/"+color,typeof(Material)) as Material;
sphere.GetComponent<Renderer>().material = m ;
}
public void CreateGem()
{
color = GemMat[Random.Range(0,GemMat.Length)];
Material m= Resources.Load("Materials/"+color,typeof(Material)) as Material;
sphere.GetComponent<Renderer>().material = m ;
}
//this is complete code when I added your suggestion the material did not attach, I also receive meshrender error as it does not exist
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class gem : MonoBehaviour {
public GameObject sphere;
public List<gem> Neighbors= new List<gem>();
string[] GemMat= {"blue","brown","green","orange","purple","red","yellow"};
string color="";
// Use this for initialization
void Start () {
CreateGem();
}
public void AddNeighbor(gem g)
{
Neighbors.Add(g);
}
public void RemoveNeighbor(gem g)
{
Neighbors.Remove(g);
}
//public void CreateGem()
//{
//color = GemMat[Random.Range(0,GemMat.Length)];
//Material m= Resources.Load("Materials/"+color,typeof(Material)) as Material;
//sphere.GetComponent<Renderer>().material = m ;
//}
public void CreateGem()
{
color = GemMat[Random.Range(0,GemMat.Length)];
// you can not set materials directly, you must get a copy, modify the copy and reassign it
Material m = Resources.Load<Material>("Materials/" + color) as Material;
Material[] materials = sphere.GetComponent<MeshRenderer>().materials;
if (materials.Length > 0)
{
materials[0] = m;
sphere.GetComponent<MeshRenderer>().materials = materials;
}
}
// Update is called once per frame
void Update(){
}
void OnMouseDown() {
Debug.Log("Clicked");
}
}