Return gameobject from a TriggerEvent.

so basicly i have an trigger collider on my player, and when i walk close to an enemy it checks if there is someting there and if its an enemy, but i’m having trouble returing the enemy’s GameObject to use it in another script.

public class TriggerTest : MonoBehaviour {
public GameObject target;
	void Start(){
		}
	
	void OnTriggerStay (Collider other)
	{	if (other.tag == "Enemy"){
		//Return the GameObject of the "other" here
		//So it can be used in other code.
		}	
	}
}

We need to see the code...

–

Oh sorry, i inserted the wrong thing!

–

1 Answer

1

You can’t strictly return anything, since you’re being called by the system. If you want a trigger to “go to” someone else, a few ways:

o Just put OnTrigger on the enemies – a collider moving into a trigger will also call the collider’s OnTrigger.

o Call the function from that other script: other.GetComponent().youRanATrigger();

o Set some vars that the other script will see: other.GetComponent().gotHit=true;

Well, Triggert = new TriggerTest(); if (Triggert.imInRange == true) isn't going to do anything useful, checking if something values of something you just made. Might be better to focus on something specific like "when I stand near an enemy, it takes damage" or something like that. Lots of already done examples here of those sorts of things.

–

That's a completely different question. But look at the TargetEnemy part. It shows you how to get the closest.

–