Search Unity

Question I wanted to change the color of material applied on prefab through script when a button is pressed

Discussion in 'Prefabs' started by MMayankk, May 19, 2020.

  1. MMayankk

    MMayankk

    Joined:
    Apr 22, 2020
    Posts:
    5
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Colorpicker : MonoBehaviour
    4. {
    5.  
    6.     public MeshRenderer enderer;
    7.     public Material green;
    8.     public Material red;
    9.     public Material blue;
    10.     public Material _default;
    11.     public Material orange;
    12.  
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         enderer =GetComponent<MeshRenderer> ();
    18.  
    19.  
    20.     }
    21.  
    22.     public void DefaultColor()
    23.     {
    24.  
    25.  
    26.         enderer.sharedMaterial.color = _default.color;
    27.  
    28.  
    29.     }
    30.  
    31.     public void BlueColor()
    32.     {
    33.  
    34.         enderer.sharedMaterial.color = blue.color;
    35.  
    36.     }
    37.  
    38.     public void RedColor()
    39.     {
    40.  
    41.         enderer.sharedMaterial.color = red.color;
    42.  
    43.     }
    44.     public void GreenColor()
    45.     {
    46.  
    47.         enderer.sharedMaterial.color = green.color;
    48.  
    49.     }
    50.  
    51.     public void OrangeColor()
    52.     {
    53.  
    54.         enderer.sharedMaterial.color = orange.color;
    55.  
    56.     }
    57.  
    58. }
    59.  

    this script works fine in editor(without any error). when I build my project(for android) this script doesn't work.
    It only changes the color of material and that's what I want.
     
    Last edited: May 19, 2020