• 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 Servebot · Aug 16, 2014 at 02:20 PM · parent transform

How I do spawn prefabs as children of another game object?

I am trying to spawn prefabs as the children of another game object so that object's transform will affect the spawned prefabs as well. I appreciate any help.

Here is my code so far:

using UnityEngine; using System.Collections;

public class Spawner : MonoBehaviour { public GameObject[] testObject; public float xRange = 1.0f; public float yRange = 1.0f; public float minSpawnTime = 1.0f; public float maxSpawnTime = 10.0f;

 // Use this for initialization
 void Start () 
 {
     Invoke ("SpawnItem", Random.Range (minSpawnTime, maxSpawnTime));
 }
 
 // Update is called once per frame
 void SpawnItem () 
 {
     float xOffset = Random.Range (-xRange, xRange);
     float yOffset = Random.Range (-yRange, yRange);
     int spawnObjectIndex = Random.Range (0, testObject.Length);
     Instantiate (testObject [spawnObjectIndex], transform.position + new Vector3 (xOffset, yOffset, 0.0f), testObject [spawnObjectIndex].transform.rotation);
     testObject [spawnObjectIndex].transform.parent = transform; 
     Invoke ("SpawnItem", Random.Range (minSpawnTime, maxSpawnTime));
 }

}

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 screenname_taken · Aug 16, 2014 at 02:38 PM 0
Share

Spawn it, and right after you spawn the prefab, call transform.parent= Your-parent-object.transform

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by PaulOrac · Aug 16, 2014 at 05:26 PM

 You are assiging the prefab as parent and not the Instance of the object you have spawned, you where close anyways, try whith this tiny modification

 using UnityEngine; 
     using System.Collections;
     
     public class Spawner : MonoBehaviour { 
     public GameObject[] testObject; 
     public float xRange = 1.0f; 
     public float yRange = 1.0f; 
     public float minSpawnTime = 1.0f; 
     public float maxSpawnTime = 10.0f;
     
     // Use this for initialization
     void Start () 
     {
         Invoke ("SpawnItem", Random.Range (minSpawnTime, maxSpawnTime));
     }
      
     // Update is called once per frame
     void SpawnItem () 
     {
         float xOffset = Random.Range (-xRange, xRange);
         float yOffset = Random.Range (-yRange, yRange);
         int spawnObjectIndex = Random.Range(0, testObject.Length);
         GameObject goInst = (GameObject) Instantiate (testObject [spawnObjectIndex], transform.position + new Vector3 (xOffset, yOffset, 0.0f), testObject [spawnObjectIndex].transform.rotation);
          goInst.transform.parent = transform;
         Invoke ("SpawnItem", Random.Range (minSpawnTime, maxSpawnTime));
     }
     }

Comment
Add comment · Show 4 · 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 Servebot · Aug 16, 2014 at 05:38 PM 0
Share

So I tried this and got an error - error CS0266: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)

avatar image PaulOrac · Aug 16, 2014 at 05:40 PM 0
Share

Veeeery sorry my friend I was indeed missing a cast hehe. I have just edited it. $$anonymous$$y apologies!

avatar image Servebot · Aug 19, 2014 at 03:02 AM 0
Share

So I tried the changes, but not it isnt spawning anything at all anymore.

avatar image Servebot · Aug 19, 2014 at 09:07 PM 0
Share

Got it working, thanks everyone!

avatar image
0

Answer by smoggach · Aug 16, 2014 at 04:55 PM

If you want your offsets to be affected by the parent position Instantiate them at Vector3.zero instead. Then afterwards apply your offsets to testObject.transform.localPosition

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Change Parents Parent from Child 1 Answer

Particle offset on parent.transform 0 Answers

Parent rotation on a BoxCollider2d 0 Answers

How to get the center of an empty parent gameobject? 1 Answer

Position of cameras children objects messed up after the game start. 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