• 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 eshonbel · May 01, 2014 at 06:00 PM · guimobileoptimizationwindows phoneaimdownsights

Aim Down Sight Script not working with GUI Button, but working with MouseButtonDown(0)

I am making a fps game for windows phone. I have an Aim Down Sight script for my gun. It works fine with Input.GetMouseButton, but when I try and use the GUI function instead, it draws it but does not$$anonymous$$ng. Here is the original script: #pragma strict

 var Aim : boolean = false; var Cam : GameObject;
 
 
 function Update () { if(Input.GetMouseButtonDown(0)){ Aim = true; if(Aim == true){ Cam.active = true; } }
 
 if(Input.GetMouseButtonUp(0)){ Aim = true; if(Aim){ Cam.active = false; }
 }
 }
 

Here is the script with GUI Functions:

 enter code here#pragma strict
 
 var Aim : boolean = false; var Cam : GameObject;
 
 
 function OnGUI () { if(GUI.Button (Rect (20,40,80,20), "ADS")){ 
 Aim = true; if(Aim == true){ Cam.active = true; } }
 
 if(GUI.Button (Rect (20,40,80,20), "ADS")){
  Aim = true; if(Aim){ Cam.active = false; }
 }
 }

I am much less confident with GUI input. I keep pressing it but it doesn't ADS like the MouseButton(0). I'd much rather have t$$anonymous$$s as a GUI functionality, as it plays weird on my phone as a tap.

Thanks. BenDy Games, Game Development For Windows Phone 8

Comment
Add comment · Show 1
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 eshonbel · May 03, 2014 at 01:31 PM 0
Share

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · May 03, 2014 at 02:14 PM

By convention, variables should start with a lower case letter. Functions and classes start with an upper case letter. Here is a bit of code that does with GUI what your original code was doing with OnMouseDown()/OnMouseUp():

 #pragma strict
  
 var aim : boolean = false; 
 var cam : GameObject;
 
 private var rect = Rect(20,40,80,20);
  
 function OnGUI () { 
 
     var e = Event.current;
 
     if (e.type == EventType.MouseDown && rect.Contains(e.mousePosition)) {
         aim = true;
         cam.SetActive(true);
         Debug.Log("Finger Down");
     }
 
     if (e.type == EventType.MouseUp && rect.Contains(e.mousePosition)) {
         aim = true;
         cam.SetActive(false);
         Debug.Log("Finger Up");
     }
     
         GUI.Button (Rect (20,40,80,20), "ADS");
 }

Note I was a bit confused about 'aim' being set to 'true' for both the mouse down and the mouse up case. Are you sure that is what you want to do? Is there more to t$$anonymous$$s script?

Comment
Add comment · Show 4 · 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 eshonbel · May 07, 2014 at 07:11 AM 0
Share
avatar image eshonbel · May 08, 2014 at 06:00 PM 0
Share
avatar image robertbu · May 08, 2014 at 06:44 PM 0
Share
avatar image eshonbel · May 09, 2014 at 06:56 AM 0
Share

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Optimize for Windows 10 Mobile 0 Answers

Is the new UI system still not recommended for mobile? (Unity 5) 1 Answer

Detecting GUI.changed using DrawDefaultInspector 3 Answers

GUI click through? 3 Answers

Disable script on 2. object by boolean from 1. object. 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