TheUnknownDev
TheUnknownDev

Reputation: 25

Get a Name from a GameObject that touches another one (2D)

Im wondering how I can get the Name from a GameObject that is touching my GameObject with my attached script.

I tried it with colliders but it won't seem to work for me.

This is the object where my script is on.

Image 1

And now I want to get the Name from this GameObject below.

Image 2

How is this possible?

Upvotes: 1

Views: 153

Answers (1)

Fattie
Fattie

Reputation: 12621

Sure, it's actually just ".name" !

So something like ...

protected void OnCollisionEnter(Collision info) {

    string theName = info.transform.name;
    Debug.Log("the name is " + theName);

Depending on your game, it's possible you'll have to understand how to use triggers (basically set the colliders to be triggers), but really that's a whole other issue.

It's also quite common to use "tags". (Just set them in the Inspector.)

protected void OnCollisionEnter(Collision info) {

    if (!info.transform.CompareTag("trees")) {

        Debug.Log("we just hit a tree, do nothing");
        return;
    }

Good luck!

Upvotes: 3

Related Questions