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 Object Reference not set to instance with GetPropertyBlock when updating other scripts

Discussion in 'Scripting' started by davidnibi, Feb 14, 2023.

  1. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    Don't worry, this isn't the common NullReferenceException question ... it is more to do with why this error popping up if I update any other script attached in the scene at runtime.

    If I update, I will get the error:

    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    The code in relation is this:

    Code (CSharp):
    1.     void CharacterTextureOffset()
    2.     {
    3.         for (int ms = 0; ms < amountOfMaterials; ms++)
    4.         {
    5.             meshren.GetPropertyBlock(_propBlock[ms]);
    6.             _propBlock[ms].SetVector(textureName, new Vector4(1, 1, tiledivision[ms] * selection[ms], 0));
    7.             meshren.SetPropertyBlock(_propBlock[ms], ms);
    8.         }
    9.     }
    This modifies an instance of a shader attached to a number of prefabs in the scene - is it the reference to meshRen or the PropertyBlock ... I've had this come up a bit and would like to ask if there's a known problem relating to this, before I add more details for it. Thanks.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Either
    meshRen
    or
    _propBlock
    or
    _propBlock[ms]
    or
    tiledivision
    or
    selection
    is null.
    Just a problem in your code, not in Unity.

    You need to identify which thing is null and why, and rectify it.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    Actually it is! When you change a script it wipes out all non-serialized properties.

    You are of course completely welcome to continue believing it isn't a bog-standard nullref, that's up to you.

    But when you are finished, you know the three steps already:

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that
     
  4. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    That's the question I was asking really - in what way does it wipe them, and why only on a script related to modifying a shader instance?
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    The process has changed many times over Unity's lifecycle but I believe they call it a domain reload now.

    And of course there is the ceaseless Microsoft talk about "edit and continue," but "Edit and continue" is approximately analogous to "Achieve world peace:" don't hold your breath.
     
    davidnibi likes this.
  6. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    So it will work if you just wipe everything and have nothing left? :D (In which case: Do hold your breath, and tell everyone else to do the same).
     
    Kurt-Dekker likes this.