Accessing Materials Through Code
Last updated
Last updated
Similar to accessing components through code, the same GetComponent<>() function can be used.
However, you aren't getting a component called "Material." Instead, you are getting the game object's Mesh Renderer to access and update its material.
In the examples below, the following variables are used:
Materials and some of their properties can be stored in variables:
In the Inspector, it appears like this, ready for game objects and assets with or of certain types:
Remember: In this example, Thing and Thing Transform fields will only accept game objects. Wood Material and Wood Texture fields will only accept assets of those types. The Selected Color field uses a color picker.
Here is the example filled in:
Now, the Mesh Renderer of a game object can be accessed and replaced with those stored in variables.
Example of code that would update the material of thing with woodMaterial:
thing.GetComponent<Renderer>().material is a lot to type each time you need to access the material of an object. So if you find yourself needing to access a game object's material properties often, you can replace that line by storing it in a public variable:
You can also assign the variable using the code:
So now, the material can be accessed with the variable.
You can store color in a public variable of Color type as seen above or update the values within the script.
The Color object takes red, green, and blue values and an optional fourth value, alpha (opacity). Numbers range from 0-1.0 by default.
To set a color, you must tell the program it's a new Color, a new set of values to create a Color object:
Examples:
You can store a texture in a public variable of Texture 2D type as seen above, but to update it in the script you use the SetTexture() function.
There are different versions of SetTexture(), but the most simple takes two arguments - the name of the variable that controls the texture as a string (in quotes) and the texture in the form of a variable.
Example:
"_MainTex" is the default variable used for the texture of Unity shaders.