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

Bug after changing material? Shader graph

Discussion in 'Shader Graph' started by IuliusCaesar, Nov 29, 2019.

  1. IuliusCaesar

    IuliusCaesar

    Joined:
    Aug 31, 2019
    Posts:
    7
    Dear community,

    Description:
    In my current project, I've got a 2D game in which enemies are rendered using the sprite renderer and a custom shader that glows (representing shields). Once an enemy is killed, I change the material to one that lets me dissolve the enemy graphically before destroying the GameObject. The dissolve amount is a vector1 within the shader.

    Error:
    I change the material with the following code, the dissolveMaterial being a [SerializeField] Material I manually set to the DissolveMaterial in the editor:
    Code (CSharp):
    1.     public void Dissolve ()
    2.     {
    3.         gameObject.GetComponent<SpriteRenderer>().material = dissolveMaterial;
    4.         dissolve = true;
    5.     }
    In the Update function, I then try to change the value of the dissolvePower. I have set the property name in the shade to "_DissolvePower" and it is shown like that when I select the shader in the editor.
    Code (CSharp):
    1.     private void Update ()
    2.     {
    3.         //check if the shields need to lowered or raised
    4.         RegulateShields();
    5.         if (dissolve)
    6.         {
    7.             gameObject.GetComponent<SpriteRenderer>().material.SetFloat  ("_DissolvePower", Time.deltaTime);
    8.         }
    9.  
    10.         if (gameObject.GetComponent<SpriteRenderer>().material.GetFloat ("_DissolvePower") >0.99f)
    11.         {
    12.             Destroy(gameObject);
    13.         }
    14.     }
    However, when I run the code, Unity tells me there is no property by the name "_DissolvePower" in the shader.... I really don't understand what's happening.

    Edit: When I manually change the DissolvePower slider at runtime, the shader works correctly. It appears to me that, for some reason, Unity cannot find the correcly named and called property. Any idea how this could be alleviated?

    Thanks a lot!
     
  2. IuliusCaesar

    IuliusCaesar

    Joined:
    Aug 31, 2019
    Posts:
    7
    Solution:
    It appears it really was a bug. I renamed the property to "_dPower" and changed the code accordingly and it started to work... really puzzling but at least it does work now.