Advertisement
Pro_Unit

RandomPlayerMaterial

Jan 26th, 2022
839
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2.  
  3. public class RandomPlayerMaterial : MonoBehaviour
  4. {
  5.     [SerializeField] private Material[] _materials;
  6.     [SerializeField] private MeshRenderer _playerMesh;
  7.  
  8.     private void Start()
  9.     {
  10.         SetRandomMaterial();
  11.     }
  12.  
  13.     [ContextMenu(nameof(SetRandomMaterial))]
  14.     private void SetRandomMaterial()
  15.     {
  16.         Material oldMaterial = _playerMesh.materials[2];
  17.         Debug.Log("oldMaterial = " + oldMaterial,oldMaterial);
  18.        
  19.         int random = Random.Range(0, _materials.Length);
  20.  
  21.         Material newMaterial = _materials[random];
  22.  
  23.         Debug.Log($"newMaterial = {newMaterial}", newMaterial);
  24.  
  25.         foreach (Material material in _playerMesh.sharedMaterials)
  26.             material.mainTexture = newMaterial.mainTexture;
  27.     }
  28. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement