Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by DFiable · Jun 14, 2011 at 08:13 AM · instantiatepositionchildhierarchy

instantiate in hierarchy

Would someone please give me an script example of instantiating a game object in a "specific place in the hierarchy? In other words I would like to create(Instantiate) a game object(like a sphere) in the child position of the main camera for example) Everytime I write a script with "Instantiate" it creates the game object outside the Hierarchy. In my game the camera is moving forward constantly and I'd like to have objects appear in the cameras view and stay in the cameras view even if I move the camera. I want them to stay in a fixed position in the cameras field of view. Thanx

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by byerdelen · Jun 14, 2011 at 10:47 AM

The best way to use for me is instantiate and make it the objects child after, I

couldn't find a better solution that comes with instantiate function and I strongly

believe it is only way and it is efficient. Example:

Make a prefab object with the script example below,

after instantiate,

camera = GameObject.Find("Camera");

instantiatedobjectsname.parent = camera.transform;

This is the main construct if I got you right, put your variables so the rest is yours.

Comment
Add comment · Show 2 · 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 DaveA · Jun 14, 2011 at 06:19 PM 1
Share

You can use Camera.main to get the camera without Find. Avoid Find if you can, or use it in Start, but never Update.

avatar image byerdelen · Jun 14, 2011 at 07:13 PM 0
Share

Oh yes, definitely a better way to use Camera.main, thanks DaveA

avatar image
0

Answer by DVFrance · Mar 07, 2015 at 07:36 PM

If I can just add a precision on byerdelen post. I'm using unity 4.6 (soon 5 :) ) and I need to wrote things like that :

 GameObject myObjectToClone;

 GameObject clone =  Instantiate(myObjectToClone) as GameObject;
 camera = GameObject.Find("Camera"); 
 clone .transform.parent = camera.transform;
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
avatar image
0

Answer by SchwertAs · Apr 08, 2017 at 11:04 PM

If you are using the editor extension feature when the game is not in run mode it is possible to insert an new object in the hierarchy permanently.

The code must be run by e. g. a button initiated procedure in a extension window. With the following steps it will do:

 //search Father Obj
 GameObject MyFatherObj = GameObjekt.Find("MyObjFatherName");
 
 // create Obj in Top hierarchy with name New Game Object
 GameObjekt myObj = new GameObject;
 
 // push down in hierarchy
 myObj.transform.parent = MyFatherObj.transform;
 
 // rename
 myObj.name = "MyObjName";

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Placing GameObject as Child in Hierarchy Changes Its Position 1 Answer

GetChild(0) returns nonexistent object 1 Answer

How to get access to child's objects of instantiated prefab on JS? 1 Answer

Problem with Parent and Child using LookAt 0 Answers

How do I get the global position 2 Answers


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