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 jordmax12 · Mar 14, 2014 at 06:16 PM · c#gameobjectraycast

how to instantiate an object that is a child of another object

I am trying to do a instantiate a game object to the child of another. It works, but every single gameobject I instantiate is in the same spot, when I want them to be in relation to their parent gameobject. The parent in my code would be "prefab" and the child that I am trying to make is "EmptyRayGameObject"

 drones = new List<GameObject>();
         for (int i = 0; i < droneCount; i++)
         {
             droneTemp = (GameObject) GameObject.Instantiate(prefab);
             EmptyRayGameObject = (GameObject) GameObject.Instantiate (ePrefab);



I have tried this, and it kind of works, but still all the gameobjects are in the same spot, as opposed to being like 10 units in front of their parent gameobject.

 EmptyRayGameObject.transform.parent = droneTemp.transform;



Answers I've tried (not succeeded)

     drones = new List<GameObject>();
     for (int i = 0; i < droneCount; i++)
     {
         droneTemp = (GameObject) GameObject.Instantiate(prefab);
         EmptyRayGameObject = (GameObject) GameObject.Instantiate(ePrefab);
         EmptyRayGameObject.transform.parent = droneTemp.transform;
         EmptyRayGameObject.transform.localPosition = new Vector3(0.5f,0.5f,0.5f);
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

1 Reply

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

Answer by robertbu · Mar 14, 2014 at 06:20 PM

A solution is to use Transform.localPosition. For example:

    drones = new List<GameObject>();
    for (int i = 0; i < droneCount; i++)
    {
      droneTemp = (GameObject) GameObject.Instantiate(prefab);
      EmptyRayGameObject = GameObject.Instantiate (ePrefab) as GameObject;
      EmptyRayGameObject.transform.parent = someOtherTransform;
      EmptyRayGameObject.transform.localPosition = new Vector3(0.5f,0.5f,0.5f);

The assigning of (0.5,0.5,0.5) to localPosition will make EmptyRayGameObject have that offset relative to the parent object.

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 jordmax12 · Mar 14, 2014 at 06:27 PM 0
Share

I tried this and I got an error for the localPosition line, so I added a "new" before Vector3 to get rid of it. However, the position of the gameobject is still always in the same spot as the others.

avatar image robertbu · Mar 14, 2014 at 09:48 PM 0
Share

Yea, you need to add 'new'. It is not necessary in Javascript, and as I switch back and forth answering questions I sometimes forget.

As for your problem. Are you sure you parented the object before you assigned the local position? Are you sure that the transform variable you are using for the parenting is not null? Is there any other code that might be positioning the object after its creation? 'localPosition' doesn't fail, so something else is going on here?

The is an alternate. Again say you wanted an offset from some other object. You can do your Instantiate like this:

  Instantiate(eprefab, someOtehrTransform.position + offset, Quaternion.identity);

This form of Instantiate() takes the position and rotation, so you are setting position as you create the object.

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

20 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

Related Questions

Getting game logic object from GameObject 1 Answer

AI Instantiate targets/prefab. (pick target issue) 1 Answer

Find if any gameObject exists in an exact Vector3? 0 Answers

Bottom area of a 3D model? Raycast? 1 Answer

compare raycast hit with list of gameobjects c# 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