Search Unity

My button.onclick won't work

Discussion in 'Immediate Mode GUI (IMGUI)' started by ayosha, Apr 10, 2015.

  1. ayosha

    ayosha

    Joined:
    Apr 7, 2015
    Posts:
    9
    Hello everybody,

    I have been struggling with that issue, did researches and tried many ways but I have troubles to set an action on the .onclick. I have no issue with the onclick trigger when it is a button defined inside the code, but here, I am working with "wysiwyg button" (click right, add GUI, button)... Among the things I tried, those three:

    1)
    Here I try to manually give my script to launch on click, but it won't launch it. I also don't understand why I don't have the same menu as all of the youtube tutorial, they usually have a full list of elements from where they can choose events, functions etc... But I just have "No function" and "Monoscript > string name".
    My script is here :
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class Nb_gobTVR : MonoBehaviour {
    7.     void test1()
    8.     {
    9.         Debug.Log ("youhouuuu");    
    10.     }
    11. }
    2) I've tried to call my button inside my script without attaching it, like this :
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5. public class Nb_gobTVR : MonoBehaviour {
    6.  
    7. public button my_butt;
    8.     void getmybutton()
    9.     {
    10.         my_butt = GetComponent<Button>();
    11.     }
    12.  
    13. void Update()
    14. {
    15.    if (my_butt)
    16.    {
    17.       Debug.Log ("youhouuuu");
    18.    }
    19. }
    20. }
    so I have two questions :

    1) How could I manage to get a reaction when the user click on it in both manners (with attaching the script to my button, and with reaching the "wysiwyg" button inside a script).

    2) In a global way, for screen scale on different devices or for any other reasons, is it better to create buttons in the code or is it ok to use (what I call) wysiwyg button ?

    Thank you for reading, :p
     
  2. Mr-Mud

    Mr-Mud

    Joined:
    Mar 8, 2015
    Posts:
    37
    Here are my thoughts on the matter:
    1. You seemingly dragged the script from your asset folder straight into the object field of the OnClick event. This is not the way you are supposed to do this. You should be able to solve this by placing you script on any GameObject, and dragging that GameObject into the specified field.

    From the code's point of view, I don't quite get what you are trying to accomplish... First you cache a reference to the button and then you test whether you have a reference to that button? My assumption is that you would like to register to that button using code. That is done as follows (don't forget to attach this script to a Button):
    Code (CSharp):
    1. protected virtual void Awake()
    2. {
    3.     Button b = GetComponent<Button>();
    4.     b.onClick.AddListener(test1);
    5. }
    6.  
    7. private void test1()
    8. {
    9.     Debug.Log ("youhouuuu");
    10. }
    2. There is no 'right' or 'wrong' in this case; go with what you are comfortable with. Though using the Editor would make more sense. My advice here is to take a look at this piece of documentation.
     
  3. ayosha

    ayosha

    Joined:
    Apr 7, 2015
    Posts:
    9
    I just want the button to do something when I click on it (knowing that the button is a wysiwyg one) :)
     
  4. ayosha

    ayosha

    Joined:
    Apr 7, 2015
    Posts:
    9
    I was too tired to try yesterday but just tried this morning and it worked perfectly!!
    thanks a lot for your answer ^.^