Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges