• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Taylord303 · Sep 27, 2012 at 01:29 AM · androidtouchgui.button

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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--;
    }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Screen moves on clicking the button 1 Answer

How to detect a touch android c# 0 Answers

Using an Android tablet as input device for a desktop app 2 Answers

Having an issue with getting my touch controls working 0 Answers

Diagonal swipe issues on Android 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges