- Home /
Found a solution
Change Object Material With a button C#
I'm trying to figure out how to change the material on an object using a button, so far I have this but I'm not fully understanding the reference to the game object that needs the material changed.
using UnityEngine;
using System.Collections;
public class VideoScreenManager : MonoBehaviour {
//Material Reference
public Material newMaterialRef01;
public Material newMaterialRef02;
public Material newMaterialRef03;
public Material newMaterialRef04;
public Material newMaterialRef05;
public Material newMaterialRef06; //Default White Material
public void GoColor01(){
gameObject.name=("VideoPlayerScreen").renderer.material.color = newMaterialRef01;
}
}
Answer by Jessespike · Feb 24, 2015 at 10:57 PM
You're trying to set a color property with a material.
Try this:
gameObject.name=("VideoPlayerScreen").renderer.material = newMaterialRef01;
I get this error Jessespike:
Assets/VideoScreen$$anonymous$$anager.cs(16,55): error CS1061: Type string' does not contain a definition for
renderer' and no extension method renderer' of type
string' could be found (are you missing a using directive or an assembly reference?)
I even tried putting a public reference to the game object and that still did not work??
$$anonymous$$aybe it did not like that my VideoPlayerScreen game object was a child of a panel on a canvas because if I drag the script that was on my game manager outside of the canvas and put in directly on the VideoPlayerScreen game object and I just do this this way it works? Well it changes my materials anyway but not doing exactly what I wanted with the Video Texture script. I was trying to see if I could get buttons to change the video screen texture that the web cam plays on because the effect is cool but it does not work the way I want.... but that is another question all together though.
public void GoColor02(){ renderer.material = new$$anonymous$$aterialRef02;
}
It works
Follow this Question
Related Questions
How to find the index of the clicked UI button? 1 Answer
Children of DualTouchControls prefab keep being forced to active 0 Answers
Changing gameobject material color makes object disappear from view. How can I fix this? 1 Answer
Material class use general question 1 Answer
DontDestory gameobject doesn't work on button click after a new scene loads 2 Answers