- Home /
Question by
Slikker · May 04, 2013 at 02:00 AM ·
guiguitexture
Activate GuiTexture When Clicking on BoxCollider c#
How can i enable a gui texture when i click on a box collider in c# ?
Comment
Answer by trs9556 · May 04, 2013 at 03:41 AM
public Camera theCamera; //set this to your camera in the inspector
public Texture2D YourImage; //set this to your image you want to show
private bool shouldIEnableTheGUITexture = false; //to determine if we need to show the image or not
void Update(){
//if there has been a mouse click
if(Input.GetMouseButtonDown(0)){
if(Physics.Raycast(theCamera.ScreenPointToRay(Input.mousePosition), out hit)){
//a collider was hit, if the name of that collider is "blah", the "blah" button was pressed!
if(hit.collider.name.Contains("THE_NAME_OF_YOUR_GAME_OBJECT_WITH_THE_COLLIDER"){
//enable gui texture
shouldIEnableTheGUITexture = true;
}
}
}
}
void OnGUI(){
if(shouldIEnableTheGUITexture){
GUI.drawTexture(new Rect(0,0,100,100), YourImage);
}
}
Only three things you need to do. Add your main camera to the inspector variable named "theCamera", change "THE_NAME_OF_YOUR_GAME_OBJECT_WITH_THE_COLLIDER" to the name of your gameobject that has the collider, and add your image in the inspector. Attach this script to any gameobject.
@trs9556 - he may be talking about a GUITexture rather than drawing a texture using GUI.DrawTexture. I wasn't sure from the question. Either way, the Raycast is a good way to go. He could also use On$$anonymous$$ouseDown() on the game object that has the box collider.