I want to do something pretty simple - there’s a script variable called frame number.
I want to modify the cutoff of the material as soon as the variable value is modified.
How can I do that?
Edit - Clarification: I realize my original question was not clear enough. I am referring to changing the public script variable from the inspector in the editor, not within the actual game…
So the solution to my specific problem, is to create a script and have it ExecuteInEditMode, so that every time something changes in the scene, I’ll set the cutoff to be the one I need according to the frame number variable.
[ExecuteInEditMode]
public class Example : MonoBehaviour {
void Update() {
FrameObject[] objects = GameObject.FindObjectsOfType(typeof(FrameObject)) as FrameObject[];
foreach (FrameObject obj in objects) {
obj.UpdateCutoff();
}
}
}
where UpdateCutoff()
calculates the cutoff using the frame number variable that is set in the inspector.
You could watch the value of frameNumber every frame in Update() and modify the cutoff if it changes. For example (using C# since you didn’t specify):
private int lastFrameNumber;
void Start() {
lastFrameNumber = frameNumber;
}
void Update() {
if (frameNumber != lastFrameNumber) {
lastFrameNumber = frameNumber;
renderer.material.SetFloat("_Cutoff", <your new value>);
}
}
However, if you know how frameNumber is going to be modified, then you could use a callback only when it’s modified, instead of checking every single frame. This is much more efficient than checking every frame. If changes aren’t very frequent, SendMessage() is the easiest option. Otherwise you can use an event delegate (in C#). Here’s a SendMessage() example:
...
frameNumber = <new value>;
<object with material>.SendMessage("OnFrameNumberChanged");
...
void OnFrameNumberChanged() {
renderer.material.SetFloat("_Cutoff", <your new value>);
}