• 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 scottfredeman · Jul 27, 2011 at 09:17 PM · instantiateparent

Problem with instantiate in a Moving Parent

Hello,

I am sorry for this question because I do see a lot of threads talking about it alot and I tried to understand them but I am unable to get my script working after about 2 days of brain thumping. I am trying to simply instantiate a number of prefabs or objects within a parent that is moving so the instantiated objects move with the parent. I am also using this random instantiate and would also like to only spawn a limited amount of prefabs or objects in this area I have specified. If I do function Start it will only spawn one object.

Thank you, Scott

Here is my script. The parent obect name is "test"

 var prefab:Transform;
 
 function Update ()
 {
   var position = Vector3(Random.Range(-150, -50), -20, Random.Range(100,2000));
 
   Instantiate(prefab, position, Quaternion.identity);
 
   prefab.transform.parent = test.transform;
    
 }
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 almo · Jul 27, 2011 at 09:21 PM 0
Share

Select your code and hit the 010101 button to format it correctly.

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by FreeTimeDev · Jul 28, 2011 at 02:30 AM

Instead of

prefab.transform (etc...)

Try:

prefab.transform.parent = transform;

Also, you probably don't want to have an instantiate in the update function like that.

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 scottfredeman · Jul 28, 2011 at 03:18 AM

Thank you for your reply. That does make it create 1 child to the parent but all the (Clones) remain outside of the parent. Is there a line of code that is missing? I tried using prefabClone.transform.parent = transform; but that just gives me errors.

Thank you Scottt

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
1

Answer by scottfredeman · Jul 28, 2011 at 01:29 PM

Ok - Your tip got me a lot closer to what I need. This Script will instantiate a Brick wall and parent it to the empty object it is attached to and if the empty is moving it will move. This is exactly what I want to do but instead of a brick wall a grid like the script after this below that does not parent properly.

Here is the Brick Wall that works correctly and all the clones become children of the empty parent it is attached to:

var cube : Transform;

function Start () {

 for (var y = 0; y < 5; y++) {
 
     for (var x = 0; x < 10; x++) {
 
         var cube = Instantiate(cube, Vector3 (x, y, 0), Quaternion.identity);
 
         cube.transform.parent = transform;            
 
     }
 }

}

Here is my Grid Script that does not work. The Clones do not become children of the empty parent: Does anyone know how to make this work as nicely as the brick wall script above?

var prefab : GameObject;

var gridX = 5;

var gridY = 5;

var spacing = 2.0;

function Start () {

 for (var y = 0; y < gridY; y++) {
 
     for (var x=0;x<gridX;x++) {
 
         var pos = Vector3 (x, 0, y) * spacing;
 
         Instantiate(prefab, pos, Quaternion.identity);
 
         prefab.transform.parent = transform;  
     }
 }

}

Thank you all, Scott

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
1

Answer by scottfredeman · Jul 28, 2011 at 02:36 PM

Ok I figured it out ! Woo Hoo! Gosh that feels good! I had to define var prefab a second time before parent. - var prefab = Instantiate(prefab, pos, Quaternion.identity);

Here is the working script that will create a grid of clones parented to the empty this script is attached to. var prefab : GameObject; var gridX = 5; var gridY = 5; var spacing = 2.0;

function Start () { for (var y = 0; y < gridY; y++) { for (var x=0;x } } }

Comment
Add comment · Show 1 · 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 FreeTimeDev · Jul 28, 2011 at 07:36 PM 0
Share

Congratulations, but don't forget to mark your answer as correct.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to Instantiate prefab as child? (Java) 1 Answer

Setting parent of instantiated object fails (Error: setting parent of prefab is disabled...) 1 Answer

Instantiated object not receiving value from parent class 2 Answers

Trying to instantiate prefabs to a parent object with C# script. 1 Answer

[SOLVED] How to make to clone an object and make it parent of the transform? 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