Get instance of the collider object

I’m new to Unity dev. So I have the character and the enemy, two objects in game. When one attacks other I can get it on event OnCollisionEnter. Great but my problem is that I don’t know how I get the instance of the object that makes that collision.

Example: My character has attacked by the enemy. I can get the tag by collision.collider.tag but I want to access it’s instance for some properties and methods I have to call.

Note: I’m using C#.

I’m assuming you want access to the component on the other game object. That is access to the public instance variables and public methods of the class you wrote and attached to the other game object. @whydoidoit’s method above will work assuming the object you collided with has that script. If it does not, you will get a null reference. Take it a piece at a time to see what is going on. Substitute whatever tag you have for “Enemy” (or you can check the name). Or you can just do he GetComponent() and check to see that the return is not null.

void OnCollision(Collision collision) {
	Debug.Log("Name: "+collision.collider.name+" Tag: "+collision.collider.tag);
	
	if (collision.collider.tag == "Enemy") {
		MyScript ms = collision.collider.GetComponent();
		if (ms == null)
			Debug.Log ("Script MyScript not attached to object");
		else
			ms.myVariable++;
	}
}