Unity Basics
  • Welcome
  • Getting Started with Unity Software
  • Helpful Shortcuts
  • The Unity Software Interface
    • The Default Interface
    • The Windows (Tabs)
      • Hierarchy Window
      • Scene Window
      • Project Window
      • Inspector Window
      • Game Window
      • Console Window
    • Other Layouts
      • Create a Custom Layout
  • Create
    • Creating Game Objects
      • 3D Primitives
      • Creating Game Objects in the Editor
      • Spawning Objects
      • Unhiding/Hiding Objects During Gameplay
      • Parenting
    • Creating Assets
    • Creating Components and Scripts
      • Adding Components in the Inspector
      • Creating Custom Components and Scripts
    • Creating Prefabs
  • Select and Update
    • General Selection
    • Search and Focus
    • Updating Game Objects
      • Updating Game Objects in the Editor
      • Updating Prefabs
    • Updating Assets
    • Updating Components
      • Update Components in the Inspector
      • Accessing Components Through Scripts
  • Delete
    • Deleting Game Objects
      • Deleting and Disabling Objects in the Editor
      • Using the Destroy() Function
    • Deleting Components and Scripts
      • Disabling and Removing Components in the Editor
      • Disabling and Removing Components During Gameplay
  • Translate, Rotate, and Scale
    • Intro to Transforms
    • Handy Transform Shortcuts
    • Translate
      • Positioning Game Objects in the Editor
      • Updating Position Through Script Code
      • Using the Transform.Translate() Function
    • Rotate
      • Rotating Game Objects in the Editor
      • Updating Rotation Through Script Code
      • Using the Transform.Rotate() Function
    • Scale
      • Resizing/Scaling Objects in the Editor
      • Updating Scale Through Script Code
    • Controlling Speed
    • Common Issues with Transforms
  • Materials
    • Material Basics
      • Creating and Applying Materials
      • Accessing Materials Through Code
  • Physics
    • Physics Basics
    • Colliders and Triggers
      • Collider Component Overview
      • Accessing Colliders Through Scripts
      • Common Issues: Colliders and Triggers
    • Rigidbody Component
      • Rigidbody Component Overview
      • Accessing the Rigidbody Component Through Scripts
      • Common Issues: Rigidbody Components
    • Common Issues: Physics and Velocity
  • Interaction
    • Interaction Basics
    • Keyboard Controls
    • Mouse Controls
  • Coding Basics
    • Intro to Scripts
    • Variables
    • Conditionals (If / Then / Else)
    • Arrays
    • Loops
    • Functions
Powered by GitBook
On this page
  • Getting the Renderer Component
  • Accessing the Material
  • Updating Color
  • Updating Textures

Was this helpful?

  1. Materials
  2. Material Basics

Accessing Materials Through Code

PreviousCreating and Applying MaterialsNextPhysics Basics

Last updated 4 years ago

Was this helpful?

Getting the Renderer Component

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:

public GameObject thing;
public Transform thingTransform;

Materials and some of their properties can be stored in variables:

public Material woodMaterial;
public Color selectedColor;
public Texture2D woodTexture;

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 = woodMaterial;

Accessing the Material

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:

public Material thingMaterial;

You can also assign the variable using the code:

thingMaterial = thing.GetComponent<Renderer>().material;

So now, the material can be accessed with the variable.

thingMaterial = woodMaterial;
thingMaterial.color = selectedColor;
thingMaterial.SetTexture("_MainTex", woodTexture);

Updating Color

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:

thingMaterial.color = new Color(0, 1, 0);   // Green at full opacity
thingMaterial.color = new Color(1, 1, 0, 0.5F);   // Yellow at half opacity

Updating Textures

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:

thingMaterial.SetTexture("_MainTex", woodTexture);

"_MainTex" is the default variable used for the texture of Unity shaders.