Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Resolved Cant replace/change material properties

Discussion in 'Scripting' started by Lifee00, May 15, 2024.

  1. Lifee00

    Lifee00

    Joined:
    Apr 13, 2021
    Posts:
    88
    Hi, I have object with 2 materials. I Want to temporarily disable the second one or replace it, make it transparent etc. Nothing worked so far. Yes I have double triple checked all names, variables, I do have "ColorInside" variable in the shadergraph for that material. I have written a test code with 2 different approaches, the debug log says "material replaced" and no errors but in fact it isnt.
    Code (CSharp):
    1.     private MeshRenderer m_renderer;
    2.  
    3.     // Name of the material you want to turn off
    4.     public string materialNameToTurnOff;
    5.  
    6.     // The replacement material
    7.     public Material replacementMaterial;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         m_renderer = GetComponent<MeshRenderer>();
    12.  
    13.         m_renderer.materials[1].SetColor("ColorInside", new Color(0, 0, 0, 0));
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         if (Input.GetKey(KeyCode.Escape))
    20.         {
    21.             MaterialChange();
    22.         }
    23.     }
    24.  
    25.     private void MaterialChange()
    26.     {
    27.         // Iterate through all materials of the Renderer
    28.         for (int i = 0; i < m_renderer.materials.Length; i++)
    29.         {
    30.             // Check if the material name matches
    31.             if (m_renderer.materials[i].name == materialNameToTurnOff)
    32.             {
    33.                 // Assign the replacement material to the renderer
    34.                 m_renderer.materials[i] = replacementMaterial;
    35.  
    36.                 Debug.Log("Material replaced.");
    37.  
    38.                 break; // Stop searching once the material is found
    39.             }
    40.             else
    41.             {
    42.                 Debug.Log("IS NOT WORK");
    43.             }
    44.         }
    45.     }
     
  2. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    838
     
    Lifee00, spiney199 and Kurt-Dekker like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,354
    Lifee00, spiney199 and Spy-Master like this.
  4. Lifee00

    Lifee00

    Joined:
    Apr 13, 2021
    Posts:
    88
    Ahh well, I never knew about that. All good and working now, thanks guys. I kinda understand now. That was great help.