- Home /
Button don't work?
Hello everyone. I've started programming with unity3D, so I am beginner and not really good. I tried to create a "Jump" button for an Android App, but everything I tried just failed. First I tried Input.touchCount, but then I could jump only one or endless times. Then I tried TouchPhase.began, but nothing happen if I click on the button. Finally I tried HitTest and again, nothing happen. I don't know any other solution. Could you help me? Code:
using UnityEngine;
using System.Collections;
public class Jump : MonoBehaviour {
public bool var;
void Update()
{
foreach(Touch touch in Input.touches)
if(this.GetComponent<GUITexture>().HitTest(touch.position))
{
var = true;
}
}
}
('var' is used in another script for jumping) Thanks!
Answer by tebandesade · May 13, 2015 at 09:15 PM
Hi well what I did here was have a Gameobject with a script called test
sing UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
public GameObject test ;
public bool toto;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
jump ();
//Debug.Log (toto);
}
public void cambiarBool()
{
if (toto) {
toto = false;
toto=true;
}
}
public void jump()
{
if (toto == false && Input.GetMouseButtonDown(0))
{
test.transform.Translate(Vector3.up );
}
}
}
That at the same time had a GameObject called Test that is the same gameobject , I referenced the GO just by dragging the GO from the Hierarchy view to the slot in the editor that whas created from the line of code "public GameObject test ; "( I like referencing GO like this) . Afterwards I created a UI.Button and in the button inspector I went to the On Click () property and below where you choose if you want (Off, editor and runetime and runetime) I referenced the gameobject that had the script Test . Next, next to the where you choose those options I listed before, you choose the script where you have your commands (in my case Test ) so I can access all the PUBLIC declared functions, in my case the cambiarBool (this in spanish means change bool) so I can afterwards proceed to jump.
I kinda did something sloppy where my bool changes from true to false ( so a "trigger" event occurs) but also, made sure that to jump, the left mouse button had to be pressed at the same time because otherwise it would move up always.
Actually you can just go ahead and skip the bool change and just add the Onclick() function to jump() and if (Input.Get$$anonymous$$ouseButtonDown(0))) ......
If I create a button, I also create a canvas and a text, but the canvas and the text need a runtime script in scene file. I don't know what I should use as runtime script, but if I delete them, I can click everywhere and I will jump. But I can't jump while I press with my other finger to control. I am a beginner and it's hard to do it the first time, so it would be nice, if you can help me.
Your answer
Follow this Question
Related Questions
How to make a script for "jetpack" boost animation 1 Answer
Something wrong with my movement script... 3 Answers
How do I access a GUITexture from C#? 0 Answers
Distribute terrain in zones 3 Answers
Unity 2D - Basic Button Movement 1 Answer