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
0
Question by karathekiller · May 04, 2016 at 05:24 AM · c#instantiatespawningchildren

Instantiate a prefab as a child of an existing object?

Hey everyone! I'm working a 3D VR escape the room game. The player goes through the room opening drawers to find the randomly spawned objects. The objects have to be parented to the animated drawers for everything to work correctly. I wrote some code to randomly spawn the items in one of three locations from an array of spawn points, but I don't know how to spawn them as children of the drawers. Any help you can offer is greatly appreciated! Thank you! Here's my code:

 public Transform[] wrenchPoints;
 public Transform[] hammerPoints;
 public Transform[] knifePoints;

 public GameObject wrench;
 public GameObject hammer;
 public GameObject knife;


 // Use this for initialization
 void Start () {
     SpawnHammer ();
     SpawnWrench ();
     SpawnKnife ();

 }
 
 // Update is called once per frame
 void Update () {
 
 }

 void SpawnHammer()
 {
     Debug.Log ("Spawning Hammer");
     int hammerSpawnIndex = Random.Range (0, hammerPoints.Length);
     Instantiate (hammer, hammerPoints [hammerSpawnIndex].position, hammerPoints [hammerSpawnIndex].rotation);
     Debug.Log(hammerPoints[hammerSpawnIndex]);
 }

 void SpawnWrench()
 {
     Debug.Log ("Spawning Wrench");
     int wrenchSpawnIndex = Random.Range (0, wrenchPoints.Length);
     Instantiate (wrench, wrenchPoints [wrenchSpawnIndex].position, wrenchPoints [wrenchSpawnIndex].rotation);
     Debug.Log(wrenchPoints[wrenchSpawnIndex]);
 }

 void SpawnKnife()
 {
     Debug.Log ("Spawning Knife");
     int knifeSpawnIndex = Random.Range (0, knifePoints.Length);
     Instantiate (knife, knifePoints [knifeSpawnIndex].position, wrenchPoints [knifeSpawnIndex].rotation);
     Debug.Log(knifePoints[knifeSpawnIndex]);
 }
Comment
Add comment · Show 1
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 KnightNobody · May 04, 2016 at 09:48 AM 0
Share

Doing this from memory but basicly.

 using UnityEngine;
 using System.Collections;
 
 public class InstantiateTHing : $$anonymous$$onoBehaviour {
 
     public GameObject prefab;
     
     // Update is called once per frame
     void Update () {
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space))
         {
             GameObject go = Instantiate(prefab, transform.position, Quaternion.identity) as GameObject;
             transform.parent = go.transform;
         }
     }
 }
 

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Munchy2007 · May 04, 2016 at 11:06 AM

You should use transform.SetParent :-

 GameObject knife;
 GameObject drawerGO;
 
 knife.transform.SetParent(drawerGO.transform, false);

If you use true for the 2nd parameter the childed object will keep it's current world position, otherwise it will be positioned relative to the new parent, based on the prefab local position value.

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

Spawning prefabs dependant upon Health UI? 2 Answers

Instantiate is throwing my throwing my objects? 1 Answer

Distribute terrain in zones 3 Answers

Instantiating at different rates 1 Answer

Player 2 not spawning 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