- Home /
how use specific for mobile button when we have 3 ?
Hi every body, i have a menu, in this menu i have 3 buttons (Play, Settings, Quit) and i want when use specific button when i touch the button on my mobile screen . i give this script for the 3 buttons, the problem is when i clik on Play the game quit, help me please .
using UnityEngine; using System.Collections;
public class NewBehaviourScript402 : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Touch touch = Input.GetTouch(s);
if(touch.phase == TouchPhase.Began)
{
Application.LoadLevel("Jeu");
}
}
}
Answer by Graham-Dunnett · May 12, 2015 at 10:48 AM
You need to check where on the screen the user taps. When you know that, you can work out which button has been pressed.
Can you give me example please because i don't understand ^^
function Update () {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Input.touchCount > 0 Input.GetTouch(0).phase == TouchPhase.Began)
{
if (Physics.Raycast (ray, hit, 100))
{
if(hit.collider.gameObject.tag == "PlayButton")
{
Debug.Log("touched play button");
Application.LoadLevel("Jeu");
}
}
}
}
Answer by Dblfstr · May 12, 2015 at 06:42 PM
Us the new UI system in unity, it handles the touch for you. Just make a Play method
public void Play(){
Application.LoadLevel("Jeu");
}
and a settings a quit method. Call those from the buttons built in "OnClick" event.
Your answer
Follow this Question
Related Questions
Problem with touch button (when moved) 2 Answers
How to get position of touch on touch screen 1 Answer
Make sphere shoot to Touch.position error 1 Answer
UI works in editor, but not on mobile device 1 Answer
Button reaction time problem 1 Answer