- Home /
Duplicate Question
Random Material
I'm a beginner programmer, and im trying to make a city building game. I thought a good place to start would be placing objects, of which I did by making a raycast from the mouse, and instantiating the object at the hit.Point The first object I have tried to do this with are trees, however they have different materials, so light green, dark green, orange etc. I then created the following script in order to change the material when the tree is instantiated, however it wont work for some reason:
using UnityEngine;
using System.Collections;
public class treeSettings : MonoBehaviour
{
public Material[] myMaterials;
void Start ()
{
gameObject.GetComponent<Renderer>().material = myMaterials[Random.Range(0,myMaterials.Length)];
}
}
I have assigned the materials to the list, but when I press play and click to instantiate the trees, it is only one colour.... what am I doing wrong? Thanks for any help :)
EDIT when I click on the cloned tree (the instantiated object,) it comes up with one of the random materials, like it is working, but it doesn't actually render as that material, ins$$anonymous$$d as a green one. Im not sure why.... Thanks :)
Follow this Question
Related Questions
Array Index Out of Range - Maze Generation Algorithm 1 Answer
Multiple Cars not working 1 Answer
Texture2D to Texture3D 2 Answers
Distribute terrain in zones 3 Answers
renderer.material doesnt work 3 Answers