Search Unity

Changing a float4 value of a shader via c# script

Discussion in 'Shaders' started by danydavidpr, Feb 1, 2017.

  1. danydavidpr

    danydavidpr

    Joined:
    Apr 2, 2015
    Posts:
    17
    hello, how can i change the values of a float4 via c# from a script

    sample code (not wortking)

    public Renderer rend;

    void Start(){
    rend.material.shader = Shader.Find(MyShaderObject");

    rend.material.......then here is where it does not work
    //the variable is a vector 4 called "_MyCustomFloat4" and is in the shader

    }

    help please :(
     
  2. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
    Code (CSharp):
    1. rend.material.SetVector("_MyCustomFloat4", new Vector4(1f, 1f, 1f, 1f));

    Please be aware that you're creating a new instance of the material and setting properties on that reference by accessing the "rend.material" property. If you'd rather set the property without creating a new instance, use "rend.sharedMaterial" instead.

    Code (CSharp):
    1. Material mat = rend.sharedMaterial;
    2. mat.SetVector("_MyCustomFloat4", new Vector4(1f, 1f, 1f, 1f));
     
    Last edited: Feb 1, 2017
  3. Nikhil11

    Nikhil11

    Joined:
    Jul 10, 2017
    Posts:
    3
    Thanks This Helped Me Lot