Im probably looking at this problem all wrong but could do with some guidance.
I have an “enemy” prefab that I duplicate multiple times. I then have a GUI element that places a sprite over the enemy and follows it around to clearly mark the enemy out for the player to see it. The problem is that this marker is instantiated when the enemy is spawned and because there are multiple identical enemies it doesn’t know which enemy it should be following.
How do I go about getting the object to set the gameObject that instantiated it as the object to follow (Sorry if thats a confusing sentence)?
If it is created at run-time and requires you to interact with a specific one, it is best that you give that instance a name (and put into a pool) so you can track it.
What may well work best in your scenario is to parent the marker to the enemy. That way it’s positioning will always be local to the enemy it’s meant to be following (also, you can simply look up parent at any moment to find the enemy of that marker).
When you instantiate the marker add:
marker.transform.parent = enemy.transform;
Then, in any marker code transform.parent will always be the enemy it’s meant to be following.