Search code examples
c#unity-game-enginegameobject

Getting name of object which is using currently a script in Unity


I'm creating GameObjects dynamically during runtime a game. Every of them has another name bust the same script. I'm looking for way to get to know which GameObject run a script and get this information in another script.

Objects are created here:

for (int i = 0; i < w.wHowManyWords; i++)
    {
        var obj = new GameObject();
        obj.name = w.wLevelWords[i, 0];
        elementName = obj.name;
...
    }

I'd like to get information about which object started this script:

public class ClickAction : MonoBehaviour, IPointerClickHandler
{
    private string[,] levelWords;

    public void OnPointerClick(PointerEventData eventData)
    {
        print("I am here");
    }
}

Solution

  • code11 already answered but if you want more control you can use this:

    Debug.Log(gameObject.name, gameObject);
    

    when you click on log message in console window it will highlight the object in hierarchy.