Choosing random material from an array
So basically I am trying to choose a random material from an array of materials and for now just display which one is picked in the console. I know how to do this with gameObjects but not sure the right words to use when referencing materials rather than objects. I attached what I came up with. Thank you! 'using System.Collections; using System.Collections.Generic; using UnityEngine;
public class gameManager : MonoBehaviour {
public Material[] Mats;
GameObject currentMat;
int index;
// Use this for initialization
void Start ()
{
Mats = Material.FindObjectsOfType(Material);
index = Random.Range(0, Mats.Length);
currentMat = Mats[index];
Debug.Log(currentMat.name);
}
// Update is called once per frame
void Update () {
}
} `
well what exactly do you want to achieve? asign the random selected material to a renderer? debugin seems to work i guess?
Answer by Justinger · Dec 14, 2018 at 07:17 PM
In start you could add this to start to print out some material info:
Debug.Log("Chosen material is " + Mats[index].name + "with color " + Mats[index].color + ".");
You could then assign this chosen material to the material or shared material of a meshRenderer.
Answer by UnityCoach · Dec 15, 2018 at 06:35 PM
currentMat
cannot be of type GameObject if you want to assign it a value from Mats
.
I'm not sure you can use FindObjectsOfType with Materials. You may have to find Renderers and get their materials.