Question by
kilian060 · Nov 23, 2017 at 07:41 PM ·
scripting problemscripting beginnerscriptingprobleminstantiatingsetcolor
Why does material.setColor not work when used in another function?,
Hey, I used a script to change the color of a block. If i use it in the Awake function, it works perfectly. But if i use it in another function (UpdateColor) it doesn't. Or did i just mess up something simple? Code:
public class RandomColor : MonoBehaviour {
[HideInInspector]
public Color c;
Renderer rend;
void Awake () {
rend = GetComponent<Renderer>();
rend.material.shader = Shader.Find("Standard (Specular setup)");
// if (s.Equals(rend.material.shader)) Debug.Log("HAY");
c = new Color(Random.Range(0, 0.15f), 0.9f, Random.Range(0, 0.15f));
rend.material.SetColor("_SpecColor", c);
}
public void UpdateColor()
{
rend.material.SetColor("_SpecColor", c);
}
}
Calling of the UpdateColor method:
GameObject o = Instantiate(block, new Vector3(x, heights[x, y] - k, y), Quaternion.identity);
o.GetComponent<RandomColor>().c = World.c_stone;
o.GetComponent<RandomColor>().UpdateColor();
Comment
Best Answer
Answer by kilian060 · Nov 24, 2017 at 10:13 PM
World.c_stone was actually new Color(140, 140, 140). But Color only allows Values between 0 and 1. So it got confused and rendered the cube black.
Your answer
Follow this Question
Related Questions
Try part of C# not being Read 1 Answer
UnityCar - Controlling Speed with a duration? 2 Answers
Script error 0 Answers
Unity CharacterController.Move Issue 0 Answers
Own player controller script let the view wobbles but why? 0 Answers