Search Unity

How to access a vector1 property in a shader graph

Discussion in 'Scripting' started by zarvin, May 9, 2019.

  1. zarvin

    zarvin

    Joined:
    Mar 28, 2017
    Posts:
    14
    Hey guys i'm working on creating a 3d UI for a vr game I'm working on and I just created this shader graph that dissolves an object. The shader graph has an exposed vector1 that should be able to change the level of dissolve an object has 0 being none and 1 being completely dissolved. Anyways i'm having a hard time accessing the vector1 in the shader. I keep getting an error saying that no material is on my object when in reality the material with my shader is on the obj. I'm sure i'm missing something simple but I can't figure it out.

    This is what i've written so far:
    Code (CSharp):
    1. void dissolve(GameObject obj, float start) {
    2.         if (start == 1f)
    3.         {
    4.             while (start > 0f)
    5.             {
    6.                 start -= Time.deltaTime * dissolveSpeed;
    7.                 obj.GetComponent<Material>().SetVector("Vector1_A780262E", new Vector4(start, 0f, 0f, 0f));
    8.             }
    9.         }
    10.         else if (start == 0f) {
    11.             while (start < 1f) {
    12.                 start += Time.deltaTime * dissolveSpeed;
    13.                 obj.GetComponent<Material>().SetVector("Vector1_A780262E", new Vector4(start, 0f, 0f, 0f));
    14.             }
    15.         }
    16.  
    17.     }
    I put a picture of my obj below along with a picture of the error i'm getting, if you have any ideas please let me know! thanks for taking the time to read this!
     

    Attached Files:

  2. DryerLint

    DryerLint

    Joined:
    Feb 7, 2016
    Posts:
    68
    I don't know if you can access the Material directly that way. Try accessing it through the MeshRenderer, thusly:
    Code (CSharp):
    1. obj.GetComponent<MeshRenderer>().sharedMaterial.SetVector()
    Also, a Vector1 is a float, isn't it? Shouldn't you be using SetFloat()?
     
  3. zarvin

    zarvin

    Joined:
    Mar 28, 2017
    Posts:
    14
    tyty this worked and yah I definitely should've been using SetFloat haha ty again.