- Home /
touching a gui texture
I need help trying to make it so that if the player touches the screen where there is a gui texture, Unity will recognize this, for example: if I have a scrolling list of things the player can choose from and the player selects on an option, I want it to change a variable from true to false, or if there is a pause button at the top of the screen and the player touches it, i want it to change a variable 'pause' from false to true
Answer by clunk47 · Sep 24, 2013 at 03:58 AM
If you are talking about an actual GUI Texture GameObject,
//C# Example
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour
{
void OnMouseDown()
{
print ("Clicked on " + guiTexture.name);
}
}
//JS Example
#pragma strict
function OnMouseDown()
{
print ("Clicked on " + guiTexture.name);
}
If you're talking about using GUI.DrawTexture
//C# Example
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour
{
public Texture2D image;
Rect rect;
Vector2 mouse;
void Awake()
{
rect = new Rect(0, 0, 256, 256);
}
void Update()
{
mouse = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
if(rect.Contains(mouse) && Input.GetMouseButtonDown(0))
print ("Clicked on image!");
}
void OnGUI()
{
GUI.DrawTexture(rect, image);
}
}
//JS Example
#pragma strict
var image : Texture2D;
private var rect : Rect;
private var mouse : Vector2;
function Awake()
{
rect = new Rect(0, 0, 256, 256);
}
function Update()
{
mouse = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
if(rect.Contains(mouse) && Input.GetMouseButtonDown(0))
print ("Clicked on image!");
}
function OnGUI()
{
GUI.DrawTexture(rect, image);
}
thanks for your input but i discovered my own solution to this
Then POST THE SOLUTION. Also, don't post comments as answers, I'll fix this one for you though.
You should give upvotes to @robertbu and myself, for taking the time to help resolve your concern as well, since our advice is of working solution.
Your answer
![](https://koobas.hobune.stream/wayback/20220613114719im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Scripting a "touch" on an object in iOS. 3 Answers
Raycast from player to touch point? 2 Answers
Detecting Multiple Touches on same frame IOS 2 Answers
Sometimes, a touch can not be determined 0 Answers
Small question about converting from Android to iOS 1 Answer