- Home /
Changing Object Texture When button is pressed.
i found a script, which when a button is pressed, the texture of an object changes:
var texture1:Texture2D;
function Update () { if(Input.GetButton("Forward") ) {
if(texture1) { Debug.Log("Texture Loaded Sucessfully..."); renderer.material.mainTexture = Texture; }
else { Debug.Log("Unable to Load texture..."); } } }
however i need this feature, but more than one texture on more than one button. I made the following changes:
var texture1:Texture2D;
var texture2:Texture2D; var texture3:Texture2D; var texture4:Texture2D;
function Update () { if(Input.GetButton("Forward") ) {
if(texture1) { Debug.Log("Texture Loaded Sucessfully..."); renderer.material.mainTexture = Texture; }
else { Debug.Log("Unable to Load texture..."); } } if(Input.GetButton("Backward") )
{
if(texture2) { Debug.Log("Texture Loaded Sucessfully..."); renderer.material.mainTexture = Texture; }
else { Debug.Log("Unable to Load texture..."); } } if(Input.GetButton("Left") )
{
if(texture3) { Debug.Log("Texture Loaded Sucessfully..."); renderer.material.mainTexture = Texture; }
else { Debug.Log("Unable to Load texture..."); } } if(Input.GetButton("Right") )
{
if(texture4) { Debug.Log("Texture Loaded Sucessfully..."); renderer.material.mainTexture = Texture; }
else { Debug.Log("Unable to Load texture..."); } } }
however this doesnt work properly. Any ideas how i can fix this?
Answer by Ejlersen · Dec 28, 2010 at 05:54 PM
Well, I think this has been answered a few times here, and efge's answer is one simple way of doing it. Here is another example (basically the same as efge's):
// Cache our renderer private var rend : Renderer;
// Store the textures in an array. Then you can quickly and more or remove some public var textures : Texture2D[];
// Awake function Awake() { // Get the renderer rend = gameObject.GetComponent(Renderer);
// If none, then add one if (rend == null) rend = gameObject.AddComponent(MeshRenderer); }
// Update function function Update() { if (Input.GetButton("Forward")) ChangeTexture(0); else if (Input.GetButton("Backward")) ChangeTexture(1); else if (Input.GetButton("Left")) ChangeTexture(2); else if (Input.GetButton("Right")) ChangeTexture(3); }
// Change texture function ChangeTexture(inx : int) { // Just make a small check, that we don't try to take a non-existing texture if (inx >= textures.Length) return;
// Change texture rend.material.mainTexture = textures[inx]; }
a quick question, since you have there ChangeTexture(0); i wonder if you an set a var type so the color would not be random
Answer by efge · Dec 28, 2010 at 05:27 PM
You have to assign the right var, e.g.:
renderer.material.mainTexture = texture1;
(not renderer.material.mainTexture = Texture; )
And maybe it would be better to make use of arrays, like this:
var MyTextures : Textrue2D[];
...
for (i=0; i< MyTextures.Length, i++) if (Texture[i]) Debug.Log("Texture" + i " Loaded Sucessfully...");
switch (Input.GetButton) { case : "Backward"; renderer.material.mainTexture = Texture[0]; break; case : ...
Answer by castleforce · Oct 13, 2011 at 01:32 AM
Hey, is there a way to make this for materials and not textures
Your answer
Follow this Question
Related Questions
Changing GameObject texture? 4 Answers
Change texture of GUItexture from script 4 Answers
Change/load a mesh texture 2 Answers
Adding Fog 3 Answers