• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by glatteinfo · Sep 20, 2021 at 07:37 PM · instantiate prefab

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image glatteinfo · Sep 20, 2021 at 08:11 PM 0
Share

thanks alot for taking the time to help me, much appreciated -> see below

avatar image
0

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);
         }
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

125 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Accessing a Function from an Instantiated Object 1 Answer

Instantiated objects, not reacting to lighting 1 Answer

PhotonNetwork.Instantiate is not "buffered" 0 Answers

Prefab instantiating at wrong position 3 Answers

Not instantiating certain prefabs in WebGL build but works fine in Unity Editor 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges