Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Lizozom · Dec 06, 2012 at 01:29 AM · instantiateprefabpositionchildinstance

How do I get the global position

I have a prefab called Section1 that is organized like this:

alt text

  • I am instantiating this prefab twice in 2 different locations.

  • Now I am printing the positions of the Child "O1" for both instances and I get the same location.

  • Why is that? How do I get their absolute global position?

This is my code:

 var prefab:GameObject;
 function Start () {
     var instance1:GameObject = Instantiate(prefab, Vector3(0.0f, 0.0f, 0.0f), transform.rotation);
     var instance2:GameObject = Instantiate(prefab, Vector3(0.0f, 0.0f, 20.0f), transform.rotation);
     var pos1:Vector3 = instance1.Find("O1").transform.position;
     var pos2:Vector3 = instance2.Find("O1").transform.position;
     Debug.Log(pos1);
     Debug.Log(pos2);
     Debug.Log(instance1.transform.position);
     Debug.Log(instance2.transform.position);
 }

And this is the output:

alt text


------------------------------------------FIXED-------------------------------

Both Dave and valorik were right. valorik said that GameObject.Find() does a global search and everytime I was doing it I was getting the same object; Dave suggested I should use `instance.transform.Find("O1").transform.position` , but that would give me a nullrefference error. I managed to fix that error by using this code:

 instance.transform.Find("obstacles").Find("O1").position;

I'm not really sure who to give the credit to. If Dave updates his answer to the code I mention above I will accept his. Thank you very much for your help.

cap.png (3.3 kB)
screen2.png (54.3 kB)
Comment
Add comment · Show 2
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 Lizozom · Dec 06, 2012 at 01:41 AM 0
Share

Yes the last two lines are right. The first two ones are the problem. They should be different vectors as the children are at different positions in space. The first two lines in the console are the positions of the child "O1" in the two instances. $$anonymous$$y reasoning says that if the parents are offset(visible in the last two lines in the debugger) then the children should be too(the first two lines);

avatar image Lizozom · Dec 06, 2012 at 01:43 AM 0
Share

I need the absolute global positions of the child "O1" for both instances and those positions should be different

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by valerik · Dec 06, 2012 at 01:48 AM

 var pos1:Vector3 = instance1.Find("O1").transform.position;
 var pos2:Vector3 = instance2.Find("O1").transform.position;

I'm not sure about this.... because if i remember correctly, find works globaly, so you are getting two times the same object... try name them in a different way.

EDIT: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html

Comment
Add comment · Show 3 · 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 Lizozom · Dec 06, 2012 at 01:55 AM 0
Share

That make sense. But what I want to do is import my prefab and then instantiate is continuously and have my player follow some generated points which are children of the instances(let's say I want my player to follow "O1" from instance to instance). Is there really no way of doing a local search?

avatar image Lizozom · Dec 06, 2012 at 01:57 AM 0
Share

Or is there maybe a way I could retrieve one of its children by name?

avatar image valerik · Dec 06, 2012 at 02:01 AM 0
Share

you can try to use: (GameObject)instance1.GetComponentInChildren("01")

or use: GameObject [] o = instance1.GetComponentsInChildren(); then you iterate on the array and search it by name. or tag. i think it works, also maybe there are more efficent solution ;)

avatar image
0

Answer by DaveA · Dec 06, 2012 at 01:43 AM

I think you want to Find off the transform, not the Gameobject:

Try this:

var pos1:Vector3 = instance1.transform.Find("O1").transform.position;

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 Lizozom · Dec 06, 2012 at 01:50 AM 0
Share

I get a NullReferenceException if I do that..

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.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Associate objects to a prefab 1 Answer

How to get the instance of a child in a prefab object? 1 Answer

I need help with the location of instatiated objects on the scene and correct interaction with this 2 Answers

Instantiate prefab as child - Won't work, don't know why. 1 Answer

Instantiated object is destroyed in scene, but still logs as existing in console. 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