Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 MilesAdamson · Oct 19, 2015 at 11:35 PM · guitexturetouch controls

Cant get touch button to work

Here is my code for a touch button. It should make a gun shoot a ball. I can't get it to successfully get pressed. I even changed the GUITexture x, y and such to cover to the entire screen. I ensured that it was properly given the gun rigidbody plus the ball prefeb. I just have no idea what is wrong

     public Rigidbody gun;
     public Rigidbody newball;
     public int shotspeed = 15;
 
     GUITexture tex;
         
     void Awake ()
     {
         tex = gameObject.GetComponent <GUITexture> ();
         tex.enabled = true;
     }
     
     
     
     
     void Update () 
     {
         if (Input.touchCount > 0) 
         {
             for(int i = 0; i < Input.touchCount; i++)
             {
                 if(tex.HitTest(Input.GetTouch(i).position) && (Input.GetTouch(i).phase == TouchPhase.Began))
                 {
                     Rigidbody cloneball;
                     cloneball = Instantiate(newball, new Vector3(gun.transform.position.x, gun.transform.position.y + 1, 0), Quaternion.identity) as Rigidbody;
                     cloneball.AddForce(new Vector3(0, 1, 0) * shotspeed, ForceMode.Impulse);
                 }
             }
         }
     }
 }
 
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 MilesAdamson · Oct 19, 2015 at 06:25 AM 0
Share

Here is my inspector if that helps,

http://imgur.com/NEJTAr9

I have tried with all sorts of pixel insets, such as the real button location, the button location with origin at lot left ins$$anonymous$$d of bottom left...

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Guppie1337 · Oct 20, 2015 at 04:22 AM

If you're trying to have an actual button on the screen as a HUD or so, try making the button from the UI objects. This will allow your user to hit the button and send any information from the press to a script. For the most part, Unity makes it really easy to implement button presses through the use of UI, but there are also a few other ways to make buttons procedural or through manipulation of colliders.

If you don't know how to use the buttons, try checking out how to use the UI Unity has worked on so hard to make your life easier over the years. Button Tutorial.

To make the button work in your script, all you have to do access the script (whether it's attached to a gameobject or not) and send in the right information.

Here's an example that will hopefully help:

 public void ShootBalls ()
 {
         Rigidbody cloneball;
 
         cloneball = Instantiate(newball, new Vector3(gun.transform.position.x,                     gun.transform.position.y + 1, 0), Quaternion.identity) as Rigidbody;
 
         cloneball.AddForce(new Vector3(0, 1, 0) * shotspeed, ForceMode.Impulse);
 }

Once you have that in the script and attached to the gameobject you want it on, you need the button to tell that to happen. Drag your gameobject on to the button component in the inspector under the "OnClick" and you will be able to access the "ShootBalls" function from the drop down. Select it and your button should be ready.

The code provided is just a clip from what you provided with the added function name and public access.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer

if statement not being accessed - why? 1 Answer

GUITexture in Unity 5 0 Answers

Gui Texture Swap? 1 Answer

Scaling a guitexture hierarchy and maintaining layout in code 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