Cannot make SetGlobalFloat work

Hello all,

I cannot make SetGlobalFloat (or any shader static function) to work, and I don’t understand why.

Here is the really simple test code I use :

using UnityEngine;
using System.Collections;

public class TestGlobalShader : MonoBehaviour {

 // Use this for initialization
 void Start () {
 m_value = 0.0f;
 }
 
 // Update is called once per frame
 void Update ()
 {
 if(Input.GetKeyDown(KeyCode.A))
 {
 m_value = Mathf.Abs(1.0f-m_value);
 Shader.SetGlobalFloat("_test", m_value);
 }
 }
 
 float m_value;
}

with this really simple shader :

Shader "EADS/TestGlobalShader" {
 Properties {
 _MainTex ("Base (RGB)", 2D) = "white" {}
 _test("_test",Float) = 0.0
 }
 SubShader {
 Tags { "RenderType"="Opaque" }
 LOD 200
 
 CGPROGRAM
 #pragma surface surf Lambert

 sampler2D _MainTex;
 float _test;

 struct Input {
 float2 uv_MainTex;
 };

 void surf (Input IN, inout SurfaceOutput o) {
 half4 c = tex2D (_MainTex, IN.uv_MainTex);
 if(_test == 1)
 {
 o.Albedo = 0;
 }
 else
 {
 o.Albedo = c.rgb;
 }
 o.Alpha = c.a;
 }
 ENDCG
 } 
 FallBack "Diffuse"
}

when hitting ‘A’, the color should turn black. It works fine when I set the “test” property manually, but not when I’m doing it with SetGlobalFloat.

Can Someone help me ?

According to the reference, Shader.setGlobal functions would only work on variables that are not exposed as a property in the property block.
So what you have to do is remove the _test(“_test”,Float) = 0.0 line to make it work.
Be sure to remake a new material, since unity will save the properties you have set on a material even when you are using a shader that doesn’t have that property.