- Home /
Question by
tryptefan · Feb 16, 2021 at 02:36 AM ·
c#shadersupdate problem
Trouble updating material property from script with SetFloat
Hello, I am trying to update a variable in a custom texture from a c# script.
Shader "UCLA Game Lab/Wireframe/Single-Sided"
Properties
{
_Color("Line Color", Color) = (1,1,1,1)
_MainTex("Main Texture", 2D) = "white" {}
_TravelY("Travel Y", Float) = 0
}
SubShader
{
Pass
{
Blend SrcAlpha OneMinusSrcAlpha
LOD 200
CGPROGRAM
#pragma target 5.0
#include "UnityCG.cginc"
#include "UCLA GameLab Wireframe Functions.cginc"
#pragma vertex vert
#pragma fragment frag
#pragma geometry geom
**float _TravelY;**
// etc
Here's is my c#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveController : MonoBehaviour
{
public Material heightMapX;
public float speedX = 0f;
public float speedY = 0f;
// Start is called before the first frame update
void Start()
{
//heightMapX.EnableKeyword("_travelX");
heightMapX.SetFloat("_TravelX", speedX);
heightMapX.SetFloat("_TravelY", speedY);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
speedX += 5f;
heightMapX.SetFloat("_TravelX", speedX);
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
speedY -= 5f;
heightMapX.SetFloat("_TravelY", speedY);
}
}
}
The value is updating in the inspector..for both the script variable and the shader property. But it doesn't update in the actual scene unless I manually change values in the inspector.
Truncated the shader code to make it easier to scan, so ignore the lack of corresponding variables.
Any idea why the scene isn't updating?
Comment