Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question I wanted to change the material of prefab through script

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

  1. MMayankk

    MMayankk

    Joined:
    Apr 22, 2020
    Posts:
    5
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
  3. 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.
     
  4. jbnlwilliams1

    jbnlwilliams1

    Joined:
    May 21, 2019
    Posts:
    267
    The only code I see here is to change the material color. What else is supposed to happen? Not even sure how this code is exected.
     
  5. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Maybe the color change methods are called by some UI button or another other script, as they are public. But it would be better if the OP author would explain what the idea/goal is.
     
  6. TwinTwinIndustries

    TwinTwinIndustries

    Joined:
    Sep 4, 2022
    Posts:
    1
    Did you manage to get it to work?