- Home /
Instantiated Gameobject Position not working
Hey there,
so I am spawning a gameobject on the event that a child of a prefab hits the ground
IEnumerator OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "ground" && checker == 0)
{
Destroy(GetComponent<CapsuleCollider>());
Destroy(GetComponent<MeshRenderer>());
Instantiate(LogOnlyOrig, gameObject.transform.position, gameObject.transform.rotation);
Then if I debug the position of the new Object, it gives me the right (new) coordinates. However, if I then send my Navmeshagent to that position, it returns to the original position where the child has spawned originally instead of where the new gameObject is now.
botGO = GameObject.Find("Bot");
bot = botGO.GetComponent<NavMeshAgent>();
bot.destination = gameObject.transform.position;
Why? And is there a way to avoid this? Any hints are much appreciated!
P.s. I have read somewhere on the internet, that instantiated gameobjects out of childs of prefabs never return their world position but rather the position within the closed prefab. Is that true? If yes, is there a workaround?
Answer by bdubbert · Sep 20, 2021 at 07:47 PM
Yes, you are not accessing the position correctly (I assume, you should provide the code where you actually tell the navmeshagent where to go).
Consider the following code:
GameObject myPrefab;
void SpawnPrefab(){
Instantiate(myPrefab, somePosition);
}
Vector3 GetPosition(){
return myPrefab.transform.position;
}
GetPosition will always return the same value, no matter where your instantiated game object position is. This is because the instantiated object is a copy of the prefab, not the prefab itself, so if the copy moves around the prefab knows nothing about it.
Instead you want to access the position of the copy of the object, like this:
GameObject myPrefab;
GameObject myInstance;
void SpawnPrefab(){
myInstance = Instantiate(myPrefab, somePosition);
}
Vector3 GetPosition(){
return myInstance.transform.position;
}
Now you will will always get the updated position of your instantiated game object, rather than the position of the prefab object.
thanks alot for taking the time to help me, much appreciated -> see below
Answer by glatteinfo · Sep 20, 2021 at 10:49 PM
Hmm i theoretically got it, but it doesn't work. Did I get it wrong?
public class LogBreakdown : MonoBehaviour
{
GameObject botGO;
NavMeshAgent bot;
public GameObject LogOnlyOrig; //the to be spawned prefab added in inspector
GameObject Log;
Vector3 botPosition;
Vector3 GetPosition()
{ return Log.transform.position;}
IEnumerator OnTriggerEnter(Collider other)
{ if (other.gameObject.name == "ground" && checker == 0)
{
Log =Instantiate(LogOnlyOrig, gameObject.transform.position, gameObject.transform.rotation);
GetPosition();
botGO = GameObject.Find("Bot");
bot = botGO.GetComponent<NavMeshAgent>();
botPosition = bot.transform.position;
bot.destination = Log.transform.position;
var distance = (gameObject.transform.position - botPosition).magnitude;
do
{ yield return null; }
while (distance > bot.stoppingDistance && bot.velocity.magnitude >= 0.1f);
}
}
}
Your answer
Follow this Question
Related Questions
Problem instantiating a prefab, and storing a reference to it. 1 Answer
How to instantiate an object on multiple positions? 0 Answers
Intantiated objects being created but being invisible...[PICTURES] 1 Answer
why is it slow to instantiate 0 Answers
instantiating prefab does not rotate child objects correctly? 1 Answer