- Home /
Android GUI Button problem
I'm having a problem with one of my GUI buttons on Android. I was able to make a pause/resume script that worked with a GUI button for touch devices. But when I try to make a GUI button that uses the Fire function in my script, it does not seem to work on the device. However, on Unity Remote the button works fine, and it works when I click on it with the mouse as well. I'm wondering if anyone can help me with this problem?
This is the pause script:
var paused : boolean = false;
var myCheck : boolean = false;
function Update () {
if(Input.GetButtonDown( "Pause" ) ) {
if (!paused) {
Time.timeScale = 0;
paused = true;
}else{
Time.timeScale = 1;
paused = false;
}
}
}
function OnGUI() {
if(GUI.Button (Rect ( 0, 20, 100, 30), "Pause")) {
Time.timeScale = 0;
paused = true;
}
if( paused ) {
if(GUI.Button (Rect ( 0, 50, 100, 30), "Resume" )) {
Time.timeScale = 1.0f;
paused = false;
}
if(GUI.Button (Rect ( 0, 90, 100, 30), "Options" )) {
}
if(GUI.Button (Rect ( 0, 130, 100, 30), "Quit" )) {
}
}
}
This is the script that calls the Fire function with a GUI button:
var Projectile: Transform;
var fireRate: float = 0.3;
private var nextFire: float = 2.0;
var Ammo : int = 30;
var gunFire : AudioClip;
var reload : AudioClip;
var empty : boolean = false;
var reloadgui : GUIStyle;
var levelloaded : boolean = true;
function Update () {
if(Input.GetButtonDown( "Shoot" )){
Fire();
}
if(Input.GetKeyUp("r")){
if(empty){
Reload();
}
}
if(Ammo <= 0){
empty = true;
}
else{
empty = false;
}
}
function Fire(){
audio.PlayOneShot(gunFire);
nextFire = Time.time + fireRate;
var shot = Instantiate(Projectile, transform.position, Quaternion.identity);
shot.rigidbody.AddForce (transform.forward * 1000);
Ammo--;
}
function Reload(){
audio.PlayOneShot(reload);
Ammo = 30;
}
function OnGUI(){
if ( levelloaded ) {
if(GUI.Button(Rect ( 0, 150, 120, 120), "Fire")) {
if(Ammo > 0){
audio.PlayOneShot(gunFire);
nextFire = Time.time + fireRate;
var shot = Instantiate(Projectile, transform.position, Quaternion.identity);
shot.rigidbody.AddForce (transform.forward * 1000);
Ammo--;
}
else{
return;
}
}
}
GUI.Box (Rect (Screen.width - 100, 20, 100, 80), "");
GUI.Label (Rect (Screen.width - 100, 35, 90, 20), "Ammo:" + Ammo);
GUI.Label (Rect (Screen.width - 100, 20, 90, 20), "Health" + playerhealth.currentHealth);
if(empty){
GUI.contentColor = Color.red;
GUI.Label (Rect (Screen.width - 100, 50, 90, 20), "RELOAD");
if(GUI.Button (Rect (10,10,30,30), "RELOAD", reloadgui)){
if(empty){
Reload();
}
}
}
}
I'm fairly new to javascript, so if this looks messy, don't be surprised. Any help would be greatly appreciated.
Answer by arsalan_lion · Oct 12, 2012 at 07:39 PM
Try Removing "audio.PlayOneShot(gunFire);" from script. It worked for me.
if(GUI.Button(Rect ( 0, 150, 120, 120), "Fire")) { if(Ammo > 0){ audio.PlayOneShot(gunFire); nextFire = Time.time + fireRate; var shot = Instantiate(Projectile, transform.position, Quaternion.identity); shot.rigidbody.AddForce (transform.forward * 1000); Ammo--; }
Try This:
if(GUI.Button(Rect ( 0, 150, 120, 120), "Fire")) {
if(Ammo > 0){
nextFire = Time.time + fireRate;
var shot = Instantiate(Projectile, transform.position, Quaternion.identity);
shot.rigidbody.AddForce (transform.forward * 1000);
Ammo--;
}
Answer by Tegleg · Jun 10, 2013 at 08:24 PM
same problem here obviously removing the audio line didnt fix it
i have a very similar but much simpler script, works fine on the preview, on pc and in web player. does not work on android
any ideas anyone? thanks
Your answer
Follow this Question
Related Questions
Screen moves on clicking the button 1 Answer
SmoothDamp touch look iOS Android 1 Answer
How to make Headbobber on joystick Android 2 Answers
Detect if finger lifted off screen 1 Answer
Dragging Camera based on Touch 0 Answers