- Home /
Scripts not updating the material's shader properties
Hello, I have a list of GameObjects and their materials that I'm trying to apply changes to via C# scripts.
I run this function every Update()
void Warp() {
foreach (GameObject obj in Objects)
{
obj.GetComponent<MeshRenderer>().sharedMaterial.SetVector("Offset", new Vector4(-10, 50, 30, 40));
Debug.Log(obj.GetComponent<MeshRenderer>().sharedMaterial.name);
}
}
According to my debug logs, it's affecting all of the GameObjects in the list properly, yet when I run it there are NO noticeable changes to the Offset vector in the game AND in the inspector.
This topic seems to explain the same issue, and a comment there suggests that it has to do with the inspector dictating the values in the shader instead of the script. However, when I close the inspector, nothing happens still.
Any ideas on why this happens?
Answer by KnightNobody · Oct 26, 2015 at 12:31 PM
In this case you should use a for loop instead of a foreach
The foreach will take an element and this will be used as a temporary variable
for(int i = 0; i <= Objects.lenght -1; i++)
{
Objects[i].GetComponent<MeshRenderer>().sharedMaterial.SetVector("Offset", new Vector4(-10, 50, 30, 40));
Debug.Log(Objects[i].GetComponent<MeshRenderer>().sharedMaterial.name);
}
This should work I think :D @bradyw24
Thanks, but it's still not working :/
It's an issue with Unity. I've also tried to update the shader values from within each Object's Update() and it still doesn't work.
Your answer
Follow this Question
Related Questions
How can I render depth into a cube render texture and sample it in another shader? 0 Answers
Shader not changing according to scene light in real time. 1 Answer
Texture2D Array Appears Grainy 0 Answers
Scriptable rendering pipeline custom rendering Order 0 Answers
How come edititng "_Glossiness" through script doesn't work? 1 Answer