- Home /
Change texure on NGUI BUTTUN press
I was wondering if somebody can help me out whit this. i am not a script-er and i'm kinda stuck on this. I wanted to make a script whit 2 variables (one for a 3d object and one for a texture) that i can assign on a Ngui button. then when pressed it, it swaps the current texture for the one in the variable. This is the script i found online and changed a bit. Only like i said i am bad at scripting and cant get it to work.
using UnityEngine; using System.Collections;
public class swap texture: MonoBehaviour { // Cache our renderer private Renderer rend;
// Store the textures in an array. Then you can quickly and more or remove some public Texture2D[] textures; public GameObject[] cube; // Awake void Awake (){ // Get the renderer rend = gameObject.GetComponent< Renderer>();
// If none, then add one if (rend == null) rend = gameObject.AddComponent< MeshRenderer>(); }
void OnClick()
{
ChangeTexture(0);
}
// Update function //void Update (){ // if (Input.GetButton("w")) // ChangeTexture(0); //else if (Input.GetButton("a")) // ChangeTexture(1); // else if (Input.GetButton("s")) // ChangeTexture(2); // else if (Input.GetButton("d")) // ChangeTexture(3); //}
// Change texture void ChangeTexture ( int inx ){ // 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]; } } As you can see originality it used the keyboard to swap the textures. And it worked fine if i assigned it to the 3D object. Only this is not a option for me cause i have multiple 3D objects that need to swap a texture and i cant use a keyboard for it.
Hope that some can help me whit this ! thanks in advanced:)