- Home /
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?
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)
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?
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
![](https://koobas.hobune.stream/wayback/20220612074436im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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