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
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);
}