Search Unity

How to get the GameObject parameter linked to a OnClick event Button

Discussion in 'Scripting' started by sputnikbar, Jan 28, 2016.

  1. sputnikbar

    sputnikbar

    Joined:
    Jan 3, 2013
    Posts:
    78
    Hi,

    I've got a GUI Panel with each button linked to a different level (I know, it's very innovative...).

    I'd like to find how I could get via script the gameObject as shown here :
    upload_2016-1-28_10-25-34.png

    My wish is that the script would return "level01".

    I've tried several things but I couldn't find anything closer than "GameManager" with this :

    Code (CSharp):
    1.     void GarnirPanelLevel(){  
    2.         Transform PL = GameObject.Find("PLevel").transform;
    3.         foreach (Transform child in PL) {
    4.             Debug.Log (child.gameObject.GetComponent<Button> ().onClick.GetPersistentTarget (0).name);
    5.         }
    6.     }
    Any idea to get the "level01" ?

    thank you,
    all the best,
    L.
     

    Attached Files:

  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    the "PersistentTarget" is the slot in the inspector to the left of the one you've highlighted, the gameobject holding the component to be run... the slot you've highlighted is the parameter of the function being called on that target.

    I suspect you need to look at: http://docs.unity3d.com/ScriptReference/Events.UnityEventBase.GetValidMethodInfo.html

    not tried it though (only thing in the onclick i can see which talks about arguements/parameters o_O).
     
  3. sputnikbar

    sputnikbar

    Joined:
    Jan 3, 2013
    Posts:
    78
    Thank you for your response.
    Yes, the thing I need is the parameter of the function being called on the target, so I've tried the following thing :

    Code (CSharp):
    1. ...
    2.             MethodInfo MI = UnityEventBase.GetValidMethodInfo (
    3.                                child.gameObject.GetComponent<Button> ().onClick.GetPersistentTarget (0),
    4.                                child.gameObject.GetComponent<Button> ().onClick.GetPersistentMethodName (0),
    5.                 new System.Type[]{ typeof(GameObject)} );
    6.  
    7.             Debug.Log (MI );
    8. ....
    and here's the console's log :

    Void AfficheLevel(UnityEngine.GameObject)
    UnityEngine.Debug:Log(Object)
    Void AfficheLevel(UnityEngine.GameObject)
    UnityEngine.Debug:Log(Object)
    Void AfficheLevel(UnityEngine.GameObject)
    UnityEngine.Debug:Log(Object)
    Void AfficheLevel(UnityEngine.GameObject)
    UnityEngine.Debug:Log(Object)
    ...

    So, how to know the value of this GameObject ( so, same problem...).
    Don't know how to figure it out.
     
  4. Henning_Justare

    Henning_Justare

    Joined:
    Nov 10, 2014
    Posts:
    21
    Did you ever find a solution to the problem?

    I'm doing an editor script and need to copy OnClick events from one button to another, but simply cant get the argument value :( It's all I need to get this to work.

    Hope you found a solution and are willing to share it..
     
    Last edited: Nov 25, 2016
  5. sputnikbar

    sputnikbar

    Joined:
    Jan 3, 2013
    Posts:
    78
  6. sirdudalot

    sirdudalot

    Joined:
    Nov 22, 2015
    Posts:
    6
    Bump...

    sputnikbar, I'd like to see your not-clean way to do this but the link you provided doesn't seem to work for me.

    Also, anyone else ever figure this one out? I've been looking for a while but no one seems to know it.

    Thanks.
     
  7. anukul

    anukul

    Joined:
    Oct 26, 2014
    Posts:
    2
    I've been looking for a solution to this same problem, to no avail :(