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. Dismiss Notice

Question Change material as instance

Discussion in 'Scripting' started by elliotbra, Jul 31, 2023.

  1. elliotbra

    elliotbra

    Joined:
    Jul 21, 2022
    Posts:
    46
    Hi,
    how do I change the material of a mesh renderer as an instance? I tried it like this
    Code (CSharp):
    1.  background.sharedMaterial = bb.background.itemMaterial;    
    2.  
    But it doesn't work. Can anyone help me?
    Here is the full script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class MatMan : MonoBehaviour
    8. {
    9.  
    10.     public Background bb;
    11.     public Renderer background;
    12.     public SpriteRenderer player;
    13.  
    14.     void Update()
    15.     {
    16.         bb = FindObjectOfType<Background>();
    17.         background = GameObject.Find("Background").GetComponent<MeshRenderer>();
    18.  
    19.         if (bb.background != null)
    20.         {
    21.             Scene currentScene = SceneManager.GetActiveScene ();
    22.  
    23.             string sceneName = currentScene.name;
    24.  
    25.             background.sharedMaterial = bb.background.itemMaterial;    
    26.        
    27.             if (sceneName == "GameScene")
    28.             {
    29.             player = GameObject.Find("Circle").GetComponent<SpriteRenderer>();
    30.             player.sprite = bb.ball.itemImage;  
    31.             }
    32.         }
    33.     }
    34. }
     
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    I'm assuming you know the difference between "material" and "sharedMaterial", if not : https://docs.unity3d.com/ScriptReference/Renderer-sharedMaterial.html

    And by "doesn't work" I'll assume you mean that the material is default in color. So my only thought is, how sure are you that referencing "bb.background.itemMaterial" is actually getting the proper material? I would debug that.

    Since you show sceneManagement, makes me wonder if the referenced material is, or has been loaded yet, or at all.

    Now on the off hand my memory serves me correctly, in some cases you need to declare something separately.
    Kinda like this for example:
    Code (CSharp):
    1. Material mat = bb.background.itemMaterial;
    2. print($"mat is {mat.name}"); // debug
    3. background.sharedMaterial = mat;
    But I'm just shooting in the dark here, so sorry if I assumed incorrectly the issue
     
  3. elliotbra

    elliotbra

    Joined:
    Jul 21, 2022
    Posts:
    46
    I mean, when I change the material in the inspector, then there is a "(Instance) After the material. but when I change it over script, it just isnt there.
     
  4. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    well, Instance can mean multiple things, but basically a copy/duplicate of a original template. So one could say what it's trying to duplicate could be bad(in some way).

    Best thing to try, is make a new material, and set it up with whatever texture, or color it is you're looking for, overall or just for a test. Declare that material within the script, and use it
    background.sharedMaterial = testMaterial01; // whatever you name it

    And see if that works. If it does, then you know the referenced material you're trying to "get" is bad, and needs fixed. And if it doesn't work, then you know something is wrong with your call "background.sharedMaterial".