Procedural Material Changer Unity Script


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 select the size, then drag and drop materials to their slots. Using this script is very easy. Just drag and drop on the object you want to change the material, 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 the 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 () {

//}
}


Comments

You are welcome to shate your ideas with us In comments

Archive

Contact Form

Send