How to get name of the wall with player collide

how can i get name of the wall or an object to which my player collides

Hi,

Welcome to our community! :slight_smile:

If you use a Unity collision method such as OnCollisionEnter or OnTriggerEnter, look the method up in the API. Then click on the parameter type. The parameter is inside the parentheses: void DoSomething (ParameterType parameterName);.

Then check the member list of the parameter type. You will either see name, or you won’t. If there isn’t any name, click gameObject. Then you will see name.

And then you could access the name of the other game object with parameterName.name and parameterName.gameObject.name respectively.

Did this help?


See also:

1 Like

thanks for answer i really appreciate it. but this will give me name of the player.in my case my collision script is attached with the obstacle and when my player collide with the obstacle i am changing the color of the obstacle by using GetComponent but i also want to get the name of the obstacle . here is the code

public class ObjectCollide : MonoBehaviour
{
private void OnCollisionEnter(Collision other)
{
Debug.Log(other.collider.name);

    GetComponent<MeshRenderer>().material.color=Color.red           
}

}

Debug.Log(other.collider.name); this line giving me the name of player not the name of obstacle …

in my case my collision script is attached with the obstacle […] i also want to get the name of the obstacle

That’s easy. :slight_smile:

Log name into your console. If that doesn’t work, try gameObject.name. Your script inherits from MonoBehaviour. That’s why your script object knows the name of the game object to which the object is attached.

1 Like

thankyou so much for helping me out its was so simple. you explain very well. solved… thanks again

You’re welcome! :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms