- Home /
How to change material of an object into material that already exist?
Hi! I know that there is a lot of topics about this but I couldn't find a topic about how to change material of an object into material that already exist. How to reach it in C#? Thanks!
Answer by HalversonS · Apr 16, 2014 at 04:01 PM
You can use Resources.Load. Make sure to create a Resources folder in your asset folder.
As an example:
// Assigns a material named "Assets/Resources/changeMaterial" to the object.
Material newMaterial = Resources.Load("changeMaterial", typeof(Material)) as Material;
gameObject.renderer.material = newMaterial;
somehow, I've got an error message that says "Load can only be called from the main thread."
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : $$anonymous$$onoBehaviour {
$$anonymous$$aterial new$$anonymous$$aterial = Resources.Load("matherialName", typeof($$anonymous$$aterial)) as $$anonymous$$aterial;
// Use this for initialization
void Start () {
}
float xtemp;
// Update is called once per frame
void Update ()
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
xtemp = Input.mousePosition.x;
}
if (Input.Get$$anonymous$$ouseButtonUp (0))
{
if (Input.mousePosition.x < xtemp)
{
Debug.Log("Left slided");
GameObject.Find("cude1").transform.renderer.material = new$$anonymous$$aterial;
}
}
}
}
Answer by robertbu · Apr 16, 2014 at 04:08 PM
Typically if the material already exists, you would use drag and drop to initialize a public variable. Attach this script to an game object. Select the object and then drag and drop the material you want to change to on the 'mat' variable.
public Material mat;
void Start() {
renderer.material = mat;
}
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
enable/disable object in heirachy from another object 1 Answer
Manipulating Instanced Object 2 Answers
Trouble grabbing and holding a cube. 0 Answers