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 yaelbsg · Apr 08, 2016 at 05:31 PM · buttonbuttonsclick

Are ui button multiple on click() functions possible?

In my Game there is a "hold" button that makes the player "carry" an object which the camera is raycasting on: on the first click it lifts the object (and the player can move around with the object 'carried' in front of him), and on the next click it drops it. i made a script of two functions for both methods - and the button has a reference for both of them in the inspector's 'Button script' "on click()" menu (which i've seen that it is able to add a bunch of functions and not just one). perhaps i don't fully understand the mean of this "on click()" menu, but the result is when i hit the button, it does both functions one by one (lifts the object and than drops it right away) in just one click.

here is my script:

 public GameObject mainCamera;
     bool carrying;
     GameObject carriedObject;
     public float distance;
     public float smooth;
 
     void rotateObject() {
         carriedObject.transform.Rotate(5,10,15);
     }
 
     void carry(GameObject o) {
         o.transform.position = Vector3.Lerp (o.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, Time.deltaTime * smooth);
         o.transform.rotation = Quaternion.identity;
     }
 
     public void pickup() {
             int x = Screen.width / 2;
             int y = Screen.height / 2;
 
             Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3(x,y));
             RaycastHit hit;
             if(Physics.Raycast(ray, out hit)) {
             Pickupable p = hit.collider.transform.parent.GetComponent<Pickupable>();
                 if(p != null) {
                 Debug.Log ("ray has detected an object");
                     carrying = true;
                     carriedObject = p.gameObject;
                     //p.gameObject.rigidbody.isKinematic = true;
                     p.gameObject.GetComponent<Rigidbody>().useGravity = false;
                 carry(carriedObject);
                 }
             }
 
 }
 
 
 
     public void dropObject() {
         if (carrying == true) {
             carrying = false;
             Debug.Log ("Dropped object");
             //carriedObject.gameObject.rigidbody.isKinematic = false;
             carriedObject.gameObject.GetComponent<Rigidbody> ().useGravity = true;
             carriedObject = null;
         }
     }
 }

i should probably mention that this script was not originally written by me, i've made a few changes in it in order to make it compatible with touch input. originally the first line in the functions "pickup()" and "dropObject()" was "if(Input.GetKeyDown (KeyCode.E))" which determines if the key e was pressed (and i assume that the on click() method supposed to put it by). i tried to convert it to that button on click() method but it doesn't seem to work seperatly. what would you suggest to do?

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

1 Reply

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

Answer by troien · Apr 08, 2016 at 06:17 PM

OnClick is an event. The way events work is that you can add listeners to them (In Unity's case, you can do this in the inspector). You say that you added 2 listeners: pickup() and dropObject(), correct?

Every time an event occurs (The onclick event in case of a button) all the listeners of the event are invoked/called. So when you click, both methods will be called like you found out.

What you want is to not have 2 methods listening to that event which overwrite each other, but just one which does one of the 2 methods instead of both. Like so:

 public void DoToggle()
 {
     if (carrying)
     {
         dropObject();
     }
     else
     {
         pickup();
     }
 }

And then add this method to the onclick in the inspector (and remove the other 2 listeners from that list)

Comment
Add comment · Show 2 · 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 yaelbsg · Apr 08, 2016 at 07:26 PM 0
Share

first of all thanks for the comment! actually i figured that this is the answer to my question right after i sent it haha, but i do have another problem with this code... i've encountered an error that the script doesn't make the object be carried all the time because there is no "update()" function that tells to do so. i thought that perhaps making the carried object a child of the charactercontroller be the solution but it gives me so many errors when i try running the game... i think it's because i try to detach the parent in the "dropobject" function but i don't know... here's the change: (i had many of the lines marked out because of the errors)

         void carry(GameObject o) {
             //o.transform.SetParent(mainCamera.transform);
             //o.transform.position = new Vector3 (0, 0, 0); 
             //o.transform.position = Vector3.Lerp (o.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, Time.deltaTime * smooth);
             o.GetComponent<Rigidbody> ().useGravity = false;
         }
     
         public void pickup() {
             if (carrying == false) {
                 int x = Screen.width / 2;
                 int y = Screen.height / 2;
     
                 Ray ray = mainCamera.GetComponent<Camera> ().ScreenPointToRay (new Vector3 (x, y));
                 RaycastHit hit;
                 if (Physics.Raycast (ray, out hit)) {
                     Pickupable p = hit.collider.transform.parent.GetComponent<Pickupable> ();
                     if (p != null) {
                         Debug.Log ("ray has detected an object");
                         carrying = true;
                         carriedObject = p.gameObject;
                         carry (carriedObject);
                     }
                 }
             } else
                 dropObject ();
     
     }
             
     
         public void dropObject() {
                 carrying = false;
                 Debug.Log ("Dropped object");
             //carriedObject.transform.parent.parent = null;
                 carriedObject.gameObject.GetComponent<Rigidbody> ().useGravity = true;
                 carriedObject = null;
     
         }
     }
     

perhaps you have another idea?

avatar image troien yaelbsg · Apr 10, 2016 at 10:32 AM 0
Share

Well, I would say making the object you want to pick up a child of the object/player that picks it up is indeed the way to go, but I don't know what the 'errors' are that you have :p

By calling something like:

 carriedObject.SetParent(yourPlayersTransformHere);

As long as you then detatch it again when dropping this should work fine. (detatching should then be done by calling 'carriedObject.SetParent' again, probably with null as parameter.

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

GUI Repeatbutton up? 1 Answer

NGUI Buttons 2 Answers

How do I get my UI Button to play my "fire animation" once? 2 Answers

This is not possible to be called for standalone input. Please check your platform and code where this is called (C#) 2 Answers

Detect if player wants to pause or to jump 3 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