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

Passing MaterialPropertyBlock from one object to another

Discussion in 'General Graphics' started by Gautzillaa, Apr 1, 2020.

  1. Gautzillaa

    Gautzillaa

    Joined:
    May 23, 2018
    Posts:
    6
    Hi everyone,

    I have an issue that I can't figure out:

    First, I don't know if it's of any importance, but I'm using the Universal Render Pipeline.

    I have 4 player GameObjects (named "Playern") which have the same material ("PlayerMat"). Each player has a proper MaterialPropertyBlock that sets its color to be specific.

    When a player shoots, I instantiate a "Ammo" GameObject, which I want to have the same color as the player. Thus, I want to access the player's MaterialPropertyBlock through the newly instantiated ammo.

    I created a SetColor method attached to the ammo prefab:

    Code (CSharp):
    1. public class AmmoColor : MonoBehaviour
    2. {
    3.     private Renderer ammoRenderer;
    4.  
    5.     void Start()
    6.     {
    7.         ammoRenderer = gameObject.GetComponent<Renderer>();
    8.     }
    9.  
    10.     public void SetColor(MaterialPropertyBlock playerBlock)
    11.     {
    12.         ammoRenderer.SetPropertyBlock(playerBlock);
    13.     }
    14. }
    that is called by the Player after instantiating the ammo:

    Code (CSharp):
    1. GameObject ammoFired = Instantiate(ammo, transform.position, transform.rotation);
    2. ammoFired.GetComponent<AmmoColor>().SetColor(playerBlock);
    The thing is that when doing so, I have a NullReferenceException:

    "NullReferenceException: Object reference not set to an instance of an object
    AmmoColor.SetColor (UnityEngine.MaterialPropertyBlock playerBlock) (at Assets/Scripts/AmmoColor.cs:19)"

    I tried to Debug.Log(playerBlock.isEmpty) inside the SetColor method, which returns false, implying the method receives the MaterialPropertyBlock.

    I went even further and tried the following:

    Code (CSharp):
    1. public void SetAmmoColor(MaterialPropertyBlock playerBlock)
    2.     {
    3.         Color playerColor = playerBlock.GetColor("_BaseColor");
    4.         Debug.Log(playerColor);
    5.  
    6.         ammoBlock.SetColor("_BaseColor", playerColor);
    7.         ammoRenderer.SetPropertyBlock(ammoBlock);
    8.     }
    The Debug efficiently displays the right color (that of the player), but the exact same NullReferenceException points at the 6th line (
    ammoBlock.SetColor("_BaseColor", playerColor);
    ), even though there is no reference to playerBlock.

    Does anyone have an idea of what I'm doing wrong?

    Thanks!

    EDIT: it seems to work if I put the 6th and 7th lines in the Update method instead of the SetAmmoColor method. I really don't know nor understand why. The thing is I don't need to update the material color each frame. I might still look forward to resolving this.
     
    Last edited: Apr 1, 2020
  2. Shaunyowns

    Shaunyowns

    Unity Technologies

    Joined:
    Nov 4, 2019
    Posts:
    328
    Hey @gautberthomieu, glad to see it works for you now, but as you mentioned if you're not sure why we should probably try find out, I'll see if I can get this post looked at!

    If you can please let me know what version you're using!
     
  3. Gautzillaa

    Gautzillaa

    Joined:
    May 23, 2018
    Posts:
    6
    Hi Shaunyowns,

    Yes it works, but since the color change isn't made directly, there still is a single frame where the ammo instance isn't coloured, just before the Update call.

    I'm using the 2019.3.7f1 version.

    Thanks for your attention!
     
  4. Shaunyowns

    Shaunyowns

    Unity Technologies

    Joined:
    Nov 4, 2019
    Posts:
    328
    You mentioned using URP, what version of that as well please?
     
  5. Gautzillaa

    Gautzillaa

    Joined:
    May 23, 2018
    Posts:
    6
    I'm using version 7.3.1