- Home /
Question by
lowen · Jun 16, 2011 at 04:51 PM ·
texturemouseclickvarmouse button
Changing texture on mouse click
Hi everybody,
I'm trying to change the texture of an object everytime I click on it with the Mouse Button.
I think it will need a sort of "var" but I can't figure out the way to make it.
Thanks
this is what I done:
using UnityEngine; using System.Collections;
public class interact : MonoBehaviour { public GUIText target;
//public Texture Square;
private bool selected = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//renderer.material.color = Color.white;
selected = false;
}
public void OnLookEnter(){
//renderer.material.color = Color.red;
target.text = "Menter code hereouse button to interact";
selected = true;
}
void OnGUI(){
Event e = Event.current;
if(e.isKey && e.character == 'e' && selected){
transform.Rotate(Vector3.up * (Time.deltaTime + 30), Space.World);
renderer.material.color = Color.red;
//rigidbody.AddForce(Vector3.up *100);
}
Event q = Event.current;
if(q.isKey && q.character == 'q' && selected){
transform.Rotate(Vector3.up * (Time.deltaTime - 30), Space.World);
renderer.material.color = Color.yellow;
}
var url0= "file://C:/****/Simple_Stone.jpg";
var url1 = "file://C:/****/Simple_Stone_Wall.jpg";
var url2 = "file://C:/****/Simple_Stone_Gate.jpg";
if(Input.GetMouseButtonDown(0)&& selected){
var bumpMap = new Texture2D(1024, 1024);
new WWW(url1).LoadImageIntoTexture(bumpMap);
renderer.material.SetTexture("_MainTex", bumpMap);
}
}
}
Comment
Answer by DaveA · Jun 16, 2011 at 08:50 PM
You don't need OnGUI for all that. Your Update function is resetting 'selected' all the time, that's no good. I'd move all that OnGUI code to Update. Use Input.GetKeyDown("e")
Your answer
![](https://koobas.hobune.stream/wayback/20220612110441im_/https://answers.unity.com/themes/thub/images/avi.jpg)