- Home /
Scripting Shortcuts
Hi, I'm learning to script, just getting used to few things around here and there.
If I wanted to use gameObject.renderer.material.color often, can't I just set the whole thing equal to "Color" somehow?
So I tried Color = gameObject.renderer.material.color
I forgot how to make those Shortcuts. Can somebody help me?
I just want to be able to use the whole sentence without having to type it out every single time. Do you get me?
I remember watching a tutorial video about it. Someone used a shortened version of narrowing things down, so for example, "Cube Color" instead of renderer.material.color.
He said that would be less messy & faster for Unity to read...
Answer by -hiTo- · Aug 11, 2013 at 03:15 PM
Color variableName = gameObject.renderer.material.color;
Not sure if this is what you are looking for, as it is pretty trivial scripting...
If on the other hand you want to CHANGE gameObject.renderer.material.color often, then no, there is no shortcut, other than defining a new Color, changing it and then setting the material.color to that newly defined Color at the end of the function.
Also, gameObject is unnecessary to type out. renderer.material.color is enough.
I just want to be able to use the whole sentence without having to type it out every single time. Do you get me?
I remember watching a tutorial video about it. Someone used a shortened version of narrowing things down, so for example, "Cube Color" ins$$anonymous$$d of renderer.material.color.
He said that would be less messy & faster for Unity to read...
Color matCol = renderer.material.color;
Debug.Log(matCol);
Debug.Log(renderer.material.color);
Both of the Log-functions will return the same value.
However, if you want to assign a value to renderer.material.color;
You make a property or function that will change the value of that property when another value is changed.
e.g.
private Color _matCol;
private Color matCol
{
get { return _matCol; }
set { _matCol = value; renderer.material.color = value; }
}
//If you do it like that, you can change
//renderer.material.color by changing matCol, like so:
private void ChangeColor()
{
matCol = new Color(0.5f, 0.5f, 0.5f);
//renderer.material.color is now 0.5f, 0.5f, 0.5f
}
But that seems to be an awful workaround just to not type three words.
$$anonymous$$ost definitely, you are making a way too big of an issue of a simple thing. And I believe the first example will be of more use than the second.