- Home /
How do I set up a GUITexture as a button? (Java Script)
I added GUITexture by going to Game object>>Create Other>>GUI Texture and added its texture in the inspector what I want now is when the user touches (Game is for android and IOS) that GUITexture to move cube up I tried this code
var up : GUITexture;
var player : Rigidbody2D;
function Update () {
for (var evt : Touch in Input.touches) {
var HitTest1 = up.HitTest(evt.position);
if (evt.phase == TouchPhase.Began) {
if(HitTest1){
player.rigidbody2D.AddForce(Vector2(-2,0));
fireButton.enabled = false;
}
}
}
}
but it didn't work :( what can I do ?
I tried this code blew as well but didn't work can some one tell me why
var up : GUITexture;
function Update () {
if(Input.touchCount > 0){
var touch: Touch = Input.touches[0];
if(touch.phase==TouchPhase.Began&&up.HitTest(touch.position)){
Debug.Log("Touch working");
}
}
}
Hi, you want the button to be on 3d object in game, or actual GUI ?
Answer by aclee · May 04, 2014 at 06:29 PM
for most devices (IOS && Android) the OnMouseDown function would work (on touch screens)
Answer by RidlingJambo · May 05, 2014 at 06:18 PM
Hi, this tutorial was really helpful for me. It is in C#, though.
Your answer
![](https://koobas.hobune.stream/wayback/20220613144230im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Move GUI elements. 0 Answers
On Screen Button Controls 0 Answers
Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer
button pressed twice / Conversation text skip 5 Answers
Overlapping GUITexture UI Buttons 2 Answers