! Change Material Value !
I have the script, where i changing the meterial on child objects. And I need to change the value of the material "Threshold" parametr to be like looping from 0 to 1 and backwords. Please try to help in this one Right now its just getting the value of threshold, not changing it
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MatSwitch : MonoBehaviour {
public Material mainMat;
public Material newMat;
public Renderer childrens;
public Shader invert;
public float min = 0.0f;
public float max = 1.0f;
static float t = 0.0f;
public float thres;
public bool isMatNew;
void Start()
{
isMatNew = false;
thres = newMat.GetFloat("_Threshold");
}
void Update () {
Renderer[] childrens;
childrens = GetComponentsInChildren<Renderer> ();
if (isMatNew == true) {
thres = Mathf.Lerp (min, max, t);
t += 0.5f * Time.deltaTime;
foreach (Renderer rend in childrens) {
var mats = new Material[rend.materials.Length];
for (var j = 0; j < rend.materials.Length; j++) {
mats [j] = newMat;
}
rend.materials = mats;
}
}
if (isMatNew == false)
{
foreach (Renderer rend in childrens) {
var mainMats = new Material[rend.materials.Length];
for (var k = 0; k < rend.materials.Length; k++) {
mainMats [k] = mainMat;
}
rend.materials = mainMats;
}
}
if (t > 1.0f)
{
float temp = max;
max = min;
min = max;
t = 0.0f;
}
}
}
Answer by Dragate · Dec 29, 2017 at 09:14 AM
You forgot to apply your value after changing it
newMat.SetFloat("_Threshold", thres);
Take a look here: https://docs.unity3d.com/ScriptReference/Mathf.PingPong.html
Does exactly what you want and will simplify your code.
I have no "SetFloat" but GetFloat function :) And when im trying to add thres in brackets it makes the error : "No overload for method "getfloat" takes 2 argument :(
You need to add the SetFloat() function. Leave GeFloat() as you had it.
thres = $$anonymous$$athf.Lerp ($$anonymous$$, max, t);
t += 0.5f * Time.deltaTime;
new$$anonymous$$at.SetFloat("_Threshold", thres);
Wow, that is really working!) Thank you Right now i just need to add pingpong ins$$anonymous$$d of lerping
I have tried ping pong And i dont know whats wrong It goes to 1f and after its just showing "NaN" and not turning back to 0 I type like this ins$$anonymous$$d of mathf.lerp :
thres = $$anonymous$$athf.PingPong(t, max);
I cannot explain why it shows up "NaN". Delete/Comment the following code you have:
if (t > 1.0f) {
float temp = max;
max = $$anonymous$$;
$$anonymous$$ = max;
t = 0.0f;
}
and replace
thres = $$anonymous$$athf.PingPong(t, max);
t += 0.5f * Time.deltaTime;
new$$anonymous$$at.SetFloat("_Threshold", thres);
with
thres = $$anonymous$$athf.PingPong(Time.time * .5f, max);
new$$anonymous$$at.SetFloat("_Threshold", thres);
Is the behaviour still weird?
It's working perfectly, thank you :) Can you explain one more thing for me? How to make it change the value between exact numbers And how to to make transition of it slower?
Your answer
Follow this Question
Related Questions
Change Material Parameters Value?? 0 Answers
Animated Icon in Unity Dropdown Menu 0 Answers
Try part of C# not being Read 1 Answer
Pathfindingsystem Script for Waypoints doesn't work. 1 Answer