Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Get the name of the Object who activate a script ?

Discussion in 'Scripting' started by Bakudan, May 8, 2015.

  1. Bakudan

    Bakudan

    Joined:
    Mar 29, 2015
    Posts:
    16
    Hello there^^.

    I have Basically in my mini game all the important scripts attached to the Main Camera.

    I got a bunch of buttons who activate some of those scripts attached to this main camera, something like
    In Button 1:
    On click
    Activate : Main Camera -> script X , Function Y

    In this script i want to get the Button name, but i have no idea how.
    In the Function Y, somethis like "this.name" give me the name of the the script attachement, here the Main Camera, but i want the "Button 1" name.

    Is there a way to get it ?

    Its to do something like this :
    Code (CSharp):
    1.     public void MenuInvoc1_OpenClose () {
    2.  
    3.         GameObject Button = ButtonWhoActivateTheScript;
    4.         for (int i=0; i < 12; i++) {
    5.              if (Button.name == "button"+i) {
    6.                  onclickInvoc = i;
    7.              }
    8.          }
    9.         StartCoroutine(MenuInvocMove (.5f));
    10.     }
    Thanks a lot^^.
     
    Last edited: May 8, 2015
  2. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    if you change your function you can set up the button name (or number) in the inspector

    Code (csharp):
    1. public void MenuInvoc1_OpenClose(int buttonNumber) {
    2.   onclickInvoc = buttonNumber;
    3.   StartCoroutine(MenuInvocMove(.5f));
    4. }
    Then the inspector looks like this:
    upload_2015-5-8_11-36-0.png
     
  3. Bakudan

    Bakudan

    Joined:
    Mar 29, 2015
    Posts:
    16
    Ho thanks a lot i never think this way, it works perfectly ^^