You can modify assets from your runtime code. Depending on the asset type and method you use, the modifications can either be temporary or permanent. Temporary modifications reset on exiting Play mode, and permanent modifications persist on exiting Play mode.
Modifications to assets at application runtime are usually temporary. For example, you might want to modify the shaderA program that runs on the GPU. More info
See in Glossary component of the material for a character to represent a temporary invincibility power-up. The shader modification in this case is not permanent and does not persist on exiting Play mode.
The following example demonstrates this by changing the shader
property of the material
component:
using UnityEngine;
public class ExampleScript : MonoBehaviour
{
private Shader invincibleShader;
void Start()
{
invincibleShader = Shader.Find("Specular");
}
public void StartInvincibility()
{
if (TryGetComponent<Renderer>(out Renderer renderer))
{
renderer.material.shader = invincibleShader;
}
}
}
On exiting Play mode, the state of the MaterialAn asset that defines how a surface should be rendered. More info
See in Glossary resets to whatever it was before entering Play mode. Accessing renderer.material
automatically instantiates the material and returns the instance. This instance is simultaneously and automatically applied to the renderer, so any changes you make aren’t permanent.
Note: If you declare a public variable that holds a reference to the material and modify that instead of modifying the renderer.material.shader
class member directly, you won’t get the benefits of automatic instantiation before the modifications are applied.
Important: The method presented here modifies actual source asset files used within Unity. You can’t undo these modifications. Use them with caution.
To avoid the material resetting on exiting Play mode, you can use Renderer.sharedMaterial. The sharedMaterial
property returns the actual asset used by the renderer.
The following example permanently changes the material to use the Specular shader and does not reset the material to the state it was in before Play mode:
using UnityEngine;
public class ExampleScript : MonoBehaviour
{
private Shader invincibleShader;
void Start()
{
invincibleShader = Shader.Find("Specular");
}
public void StartInvincibility()
{
if (TryGetComponent<Renderer>(out Renderer renderer))
{
renderer.sharedMaterial.shader = invincibleShader;
}
}
}
The previously described programming pattern of using local or shared properties for temporary or permanent changes respectively applies for the following asset types:
Asset type | Property for temporary changes | Property for permanent changes |
---|---|---|
Material | Renderer.material |
Renderer.sharedMaterial |
MeshThe main graphics primitive of Unity. Meshes make up a large part of your 3D worlds. Unity supports triangulated or Quadrangulated polygon meshes. Nurbs, Nurms, Subdiv surfaces must be converted to polygons. More info See in Glossary |
MeshFilter.mesh |
MeshFilter.sharedMesh |
Physics MaterialsA physics asset for adjusting the friction and bouncing effects of colliding objects. More info See in Glossary |
Collider.material |
Collider.sharedMaterial |
Note: If you declare a public variable of any of these classes and make modifications to the asset using that variable instead of using the relevant class member, you won’t get the benefits of automatic instantiation before the modifications are applied.
The following asset types are not automatically instantiated when modified:
Any modifications made to these assets from code are always permanent, and can’t be undone. If, for example, you change a terrainThe landscape in your scene. A Terrain GameObject adds a large flat plane to your scene and you can use the Terrain’s Inspector window to create a detailed landscape. More info
See in Glossary’s heightmapA greyscale Texture that stores height data for an object. Each pixel stores the height difference perpendicular to the face that pixel represents.
See in Glossary or the pixelsThe smallest unit in a computer image. Pixel size depends on your screen resolution. Pixel lighting is calculated at every screen pixel. More info
See in Glossary of a texture file from code, you’ll need to account for instantiating and assigning values manually.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.