Getting the current gameobject.

I have a set of 7 gameobjects. Whenever i am clicking the next button all gameobjects are moving in x direction, so only 1 gameobject is seen through the camera. And in the same position i have put a trigger. In that trigger it should detect which game object has entered the trigger. I am using this trigger script.

void OnTriggerEnter(Collider collider) 
	{
		this.gameObject.renderer.material.name = collider.gameObject.name;
}

I dont know whether this is wrong or right but it is not even detecting the gameobject name.

To fire a trigger, an object has to have a rigidbody component, even if it's kinematic. Do your game objects have a rigidbody component attached?

"this." and both instances of "gameObject." are useless in your code.

I totally forgot to add rigidbody into it. Thanks its working fine now.

1 Answer

1

To detect a gameObject that has entered the trigger, assuming the triggering object meets all the requirement to trigger it (rigidbody, collider), do the following:

GameObject detectedGO;

void OnCollisionEnter(Collider other)
{
    detectedGO = other.gameObject;

    // do something with detectedGO.
}

I don’t know why you’d want to set a material name to a game object’s name, but perhaps you’ve created a case where it makes sense.