- Home /
Getting the transform.Postion off a instantiated object
I am making a basic RTS game and i have an enemy in the world that must be able to detect the players units. The player units are instantiated objects that are in a List of gameobjects
public List<GameObject> pooledObjects;
pooledObjects = new List<GameObject>();
GameObject tmp;
for (int i = 0; i < amountToPool; i++)
{
if (pooledObjects.Count <= maxAllies)
{
tmp = Instantiate(objectToPool);
tmp.SetActive(true);
pooledObjects.Add(tmp);
}
}
Currently how i am detecting the player is:
void PlayerDetection()
{
if(Vector3.Distance(gameObject.transform.position, playerScript.agent.transform.position) < 13f)
{
enemy.destination = GameObject.FindGameObjectWithTag("Player").transform.position;
}
else if (Vector3.Distance(gameObject.transform.position, playerScript.agent.transform.position) > 13f)
{
enemy.destination = gameObject.transform.position;
}
}
But doesnt work with the instatiated objects.
void AllyDetection()
{
if(Vector3.Distance(gameObject.transform.position, ally.transform.position) < 13f)
{
enemy.destination = ally.transform.position;
}
else if (Vector3.Distance(gameObject.transform.position, ally.transform.position) > 13f)
{
enemy.destination = gameObject.transform.position;
}
}
Finding ally with:
ally = GameObject.FindGameObjectWithTag("ally").GetComponent<GameObject>();
Answer by s-s-s-s-ss-s · Nov 06, 2021 at 01:24 PM
Have you tried instantiating "tmp" as a gameobject? Looks something like:
tmp = instantiate(ObjectToPool) as GameObject;
then you should be able to reference the GameObject's transform.position by using tmp.transform.position.
Your answer
Follow this Question
Related Questions
How to carry over variable (transform.position) and use it as Vector3 for instanitating a prefab 2 Answers
hello there I am Trying to Instantiate a 1 of 5 GameObjects 0 Answers
Change animation at runtime with Mecanim 0 Answers
Instantiation in a negative position relative to player 0 Answers
Gizmos render with an offset, but editor still displays the "right" transform.position 0 Answers