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 /
  • Help Room /
avatar image
0
Question by jordaneden · May 07, 2016 at 01:04 PM · c#prefabspawnparentchild

How do I spawn an array of prefab clones as children of an object?

I'm currently using the following to spawn the "spawnClone" prefabs where they need to be and it is doing that just fine, however, I need them parented to "spawnLocations" and am unsure as to how.

 using UnityEngine;
 using System.Collections;
 
 public class Structure_Spawner : MonoBehaviour {
 
     public Transform[] spawnLocations;
     public GameObject[] spawnPrefab;
     public GameObject[] spawnClone;
 
 
     void Start(){
         spawnSomething();
     }
 
     void spawnSomething(){
         spawnClone[0] = Instantiate(spawnPrefab[0], spawnLocations[0].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
         spawnClone[1] = Instantiate(spawnPrefab[1], spawnLocations[1].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
         spawnClone[2] = Instantiate(spawnPrefab[2], spawnLocations[2].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
         spawnClone[3] = Instantiate(spawnPrefab[3], spawnLocations[3].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
         spawnClone[4] = Instantiate(spawnPrefab[4], spawnLocations[4].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
         spawnClone[5] = Instantiate(spawnPrefab[5], spawnLocations[5].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
         spawnClone[6] = Instantiate(spawnPrefab[6], spawnLocations[6].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
         spawnClone[7] = Instantiate(spawnPrefab[7], spawnLocations[7].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
     }
 
 }
 
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 TBruce · May 07, 2016 at 02:08 PM

@jordaneden

This is a simple system that does what you are looking for but using Lists instead of Arrays (more efficient)

I am sorry that I missed that.

See my updated code below

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Structure_Spawner : MonoBehaviour
 {
     public int numberOfItemsToSpawn = 7; // modifiable in the inspector
     public List<Transform> spawnLocations = new List<Transform>();
     public List<GameObject> spawnPrefab = new List<GameObject>();
     public List<GameObject> spawnClone = new List<GameObject>();
 
     void Start()
     {
         for (int i = 0; i < numberOfItemsToSpawn; i++)
         {
             if ((i < spawnPrefab.Count) && (spawnPrefab[i] != null) &&
                 (i < spawnLocations.Count) && (spawnLocations[i] != null))
             {
                 if (spawnLocations[i].parent != null)
                 {
                     // this line spawns a prefab a an objects position
                     spawnClone[i] = Instantiate(spawnPrefab[i], spawnLocations[i].parent.position, Quaternion.Euler(0, 0, 0)) as GameObject;
 
                     // this line makes the spawned prefab a child of the parent the prefab was spawned from
                     spawnClone[i].transform.SetParent(spawnLocations[i].parent, false);
                 }
             }
             else // this can else block can be removed
             {
                 if (i >= spawnPrefab.Count)
                     Debug.Log("spawnPrefab.Count = " + spawnPrefab.Count + " - Trying to spawn item number " + (i + 1)); // i is zero based
                 else if (spawnPrefab[i] == null)
                     Debug.Log("spawnPrefab[" + i "] is null";
 
                 if (i >= spawnLocations.Count)
                     Debug.Log("spawnLocations.Count = " + spawnLocations.Count + " - Trying to spawn item number " + (i + 1)); // i is zero based
                 else if (spawnLocations[i] == null)
                     Debug.Log("spawnLocations[" + i "] is null";
             }
         }
     }
 }
Comment
Add comment · Show 6 · 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 jordaneden · May 07, 2016 at 02:41 PM 0
Share

What about parenting though? They spawn to the transforms assigned, but those transforms move and I need a way to attach the clones to them.

avatar image jordaneden · May 08, 2016 at 12:39 PM 0
Share

The parenting side works, however the "spawnClones" are parented to the object containing the script (rather their individually assigned "spawnLocations") and spawn at its location, rather than their assigned "spawnLocations".

avatar image TBruce jordaneden · May 08, 2016 at 05:27 PM 0
Share

@jordaneden

Ins$$anonymous$$d of this

 spawnClone[i].transform.SetParent(spawnLocations[i].parent, false);

do this

 spawnClone[i].transform.SetParent(gameObject.transform, false);
avatar image jordaneden TBruce · May 09, 2016 at 02:52 AM 0
Share

Still doesn't work, but I've wasted enough of your time. Thank you for all your help.

Show more comments
Show more comments

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

155 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 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

Parenting objects with code not working 2 Answers

How to save as prefab when a button is pressed 1 Answer

Objects lose collision when set as child? 0 Answers

Moving grabbed object OnTriggerStay2D 0 Answers

Why does importing a mesh create a parent. 0 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