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 FakeYou · Nov 15, 2010 at 06:58 PM · gameobjectprefabreplaceempty

Replacing empty gameobjects with prefabs

I have a prefab with some empty gameobjects as anchor points for other prefabs. I want to replace there empty gameobject with a random prefab from an array, keeping the position, rotation and scale.

This way I hope to achieve 'randomly' generated blocks.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Antony-Blackett · Mar 28, 2011 at 09:15 PM

You can do this with a script by going:

public GameObject[] prefabs; // assign these in the editor.

void GenerateRandomBlocks() {
foreach(Transform child in transform) { int randomIndex = Random.Range(0,prefabs.length - 1); GameObject randomBlock = Instantiate(prefabs[randomIndex], child.transform.position, child.transform.rotation); // this creates a new prefab of a random type at the transform of the child object. randomBlock.transform.localScale = child.transform.localScale; // this applies the child's scale to the new object. randomBlock.parent = transform; // this assigns the new object as a child of the child's parent. } }

Then place this script on the object that contains all the locators for random blocks

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 Lost_Echo · Jan 31, 2016 at 09:54 PM 0
Share

I'd really like to use this script, but the "prefabs.length" as well as the "randomblock.parent" show up as undefined in monodevelop, is there something in the script that I need but didn't have already?

avatar image
0

Answer by toddisarockstar · Jan 31, 2016 at 10:18 PM

 var all:GameObject[];
 all=GameObject.FindObjectsOfType(GameObject);//get objects in scene
 
 var i :int;
 var fab0:GameObject;//<--drag n drop you block prefabs here in the inspector
 var fab1:GameObject;
 var fab2:GameObject;
     
 var fabs:GameObject[];
           fabs=new GameObject[3];
     
     fabs[0]=fab0;
     fabs[1]=fab1;
     fabs[2]=fab2;
 for (var obj:GameObject in all){
 
 //put your spawn point objects in the scene with the name "marker"!!!
  
 if(obj.transform.name.Contains("marker"){    
     i=Random.Range(0,3);
     Instantiate(fabs[i],obj.transform.position, fabs[i].transform.rotation);}
     }
 

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 Mekiah · Feb 05, 2016 at 02:49 AM

try makeing the prefab a child object of the empty gameobject

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GameObject Initialize or Replace with prefab 0 Answers

Load Prefabs in Array with Javascript 1 Answer

Instantiating to gameobject.transform.position 1 Answer

Sporadic Failure of GetComponentInChildren 0 Answers

Set Rigidbody variable to Prefab using code 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