This script allows you to randomly assign a material to your object when a scene started. Like you want to create or spawn buildings and want to change the texture of every building. This script helps you a lot in this perspective. You can set as many materials for your objects. You can set the size, then drag and drop materials to its slots. Using this script is very easy. Just drag and drop on the object you want to change the material, then set size, and drag-drop materials in array slots. Run the application and viola. Create a file named randommaterial.CS, then copy and paste code from block given below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class randommaterial : MonoBehaviour {
public Material[] totalmaterial;
int materialcount;
// Use this for initialization
void Start () {
materialcount = totalmaterial.Length;
Renderer rend = GetComponent<Renderer>();
rend.material = totalmaterial[Random.Range(0,materialcount)];
}
// Update is called once per frame
//void Update () {
//}
}
using System.Collections.Generic;
using UnityEngine;
public class randommaterial : MonoBehaviour {
public Material[] totalmaterial;
int materialcount;
// Use this for initialization
void Start () {
materialcount = totalmaterial.Length;
Renderer rend = GetComponent<Renderer>();
rend.material = totalmaterial[Random.Range(0,materialcount)];
}
// Update is called once per frame
//void Update () {
//}
}
No comments:
Post a Comment