- Home /
The question is answered, right answer was accepted
Changing gameObjects material on trigger?
I am playing around in Unity and trying to learn the coding. I am using C#. I am somewhat familiar with it.
I am trying to set up a trigger that will change the material on a cube with this code :
void OnTriggerExit(Collider Player) {
GameObject.Find ("Cube").rigidbody.useGravity = true;
GameObject.Find ("Cube").renderer.material = Spark;
}
The useGravity works fine. But the problem with changing the material to a new material is that the new material is not exist in that context. So how do I solve this? How do I get access to the materials in when programming?
You could load it using Resources.Load Otherwise, store Spark as a public $$anonymous$$aterial and set it in the inspector
Answer by darksider2000 · May 13, 2013 at 03:29 PM
You need to initialize the Spark material in your code to access it.
Try adding this to your code:
public Material spark;
Then just drag your material into the script.
Follow this Question
Related Questions
How to change material of gameobject using C# to a material asset. 2 Answers
Player Material Change works on shop preview menu, does not change Player material on other scenes. 0 Answers
Change material GameObject locally 1 Answer
Change the material on an Object in a script? 2 Answers
How to change the material in the game, depending on an event? 1 Answer