Edit Shader from C# Script

Hello,

first of all: I’m a beginner in Unity and Shaders…

I want to edit the “_Amout” float value in this shader which is attached to an object:

Shader “Example/Normal Extrusion” {
Properties {
_MainTex (“Texture”, 2D) = “white” {}
_Amount (“Extrusion Amount”, Range(-1,10)) = 0.5
}
SubShader {
Tags { “RenderType” = “Opaque” }
CGPROGRAM
#pragma surface surf Lambert vertex:vert
struct Input {
float2 uv_MainTex;
};

float _Amount;

void vert (inout appdata_full v) {
v.vertex.xyz += v.normal * _Amount;

}
sampler2D _MainTex;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
}
ENDCG
}
Fallback “Diffuse”
}

I attached to the same object a script with the following line:

mat.SetFloat(“_Amp”, 10.0F);

mat is public variable…

If i run the script i get a shader error and the object turns purple…
I think its not so diffucult but i’m obviosly too stupid to find the answer.

Thanks in advance and greetings.

Shouldn’t you be using mat.SetFloat(“_Amount”, 10.0F);?

ups… yes i meant this…

but object stays purple… :confused:

Can you post the shader error here :slight_smile:

the console is empty… in the inspector shader area is only written “Hidden/InternalErrorShader”

ok solved. Thanks <3

Shader “Example/Normal Extrusion” {
Properties {
_MainTex (“Texture”, 2D) = “white” {}
_Amount (“Extrusion Amount”, Range(-1,10)) = 0.5

_Amp (“Amplitude”, Float) = 0.0

}
SubShader {
Tags { “RenderType” = “Opaque” }
CGPROGRAM
#pragma surface surf Lambert vertex:vert
struct Input {
float2 uv_MainTex;
};

float _Amount;
float _Amp;

void vert (inout appdata_full v) {
v.vertex.xyz += v.normal * _Amount;
// v.vertex.xy += v.normal * _Amp ;

}
sampler2D _MainTex;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
}

ENDCG
}
Fallback “Diffuse”
}

using UnityEngine;
using System.Collections;

public class Extrusion : MonoBehaviour
{
public Renderer rend;
public float amp;
void Start()
{
rend = GetComponent();
//rend.material.shader = Shader.Find(“VertexEx”);
}
void Update()
{
amp = 2.0F;
rend.material.SetFloat(“_Amount”, amp);
}
}

1 Like