• 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
Question by jacksterooney · Jun 30, 2014 at 06:15 PM · instantiateprefabprefabsproceduralgeneration

Unity not Instantiating Prefabs Properly

I'm creating a dungeon generator, where a corridor piece (as a prefab) is created, and then checks if there's anyt$$anonymous$$ng in front of it before instantiating another piece.

However, the prefab created isn't always the prefab I have in my library, and I have no idea why. Shouldn't they always be equal?

Here is the prefab, expanded in my library:

alt text

And here is the view of my $$anonymous$$erarchy, after the first piece is instantiated:

alt text

As you can see, the 'room_collider' piece is missing, why would t$$anonymous$$s be?

capture.png (9.6 kB)
capture2.png (3.1 kB)
Comment

People who like this

0 Show 7
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 Imagineer · Jun 30, 2014 at 06:26 PM 0
Share

Need to see the code also to be sure...

avatar image jacksterooney · Jun 30, 2014 at 06:36 PM 0
Share

Sure, here's the code for the first room generated.

 static var room_number = 15;
 static var closet_number = 3;
 static var elevator_number = 1;
 
 var prefab1 : GameObject;
 var prefab2 : GameObject;
 var prefab3 : GameObject;
 
 var prefabList = new Array ();
 
 var room_connect : Transform;
 
 //Create a room on the room_connect, selecting from the list
 function createRoom () {
 
     var prefabIndex = UnityEngine.Random.Range(0,prefabList.length - 1);
     Instantiate(prefabList[prefabIndex], room_connect.position, room_connect.rotation);
                 
     room_number = room_number - 1;
     Debug.Log("Room Created! Rooms Remaining: " + room_number);
     
 }
 
 function Start () {
 
     // Add prefab rooms to Array
     prefabList.push (prefab1);
     prefabList.push (prefab2);
     prefabList.push (prefab3);
 
     createRoom();
 }
avatar image jacksterooney · Jun 30, 2014 at 06:37 PM 0
Share

The pieces of corridor were assigned to each prefab slot respectively.

avatar image Lo0NuhtiK · Jun 30, 2014 at 07:15 PM 0
Share

You should use built-in arrays instead of what you're doing.

eg : var prefabs : GameObject[] ; <--can be set in inspector drag/drop

Also, Random.Range(int, int) is exclusive of max. [(0,3) will return 0 || 1 || 2] So you use prefabs.length without the (-1), else your random number will never be the last index of the array [(0,3-1) will return 0 || 1].

avatar image jacksterooney · Jun 30, 2014 at 08:33 PM 0
Share

Thanks the new code is a lot cleaner:

 //Set variables for room numbers needed
 static var room_number = 15;
 static var closet_number = 3;
 static var elevator_number = 1;
 
 //Array of possible rooms
 var prefabList : GameObject[];
 
 //Transform of the spawn point of the next room
 var room_connect : Transform;
 
 //Function to create a room on the room_connect, selecting from the list
 function createRoom () {
 
     //Create a random room from a random position on the list 
     var prefabIndex = UnityEngine.Random.Range(0,prefabList.length);
     Instantiate(prefabList[prefabIndex], room_connect.position, room_connect.rotation);
         
     //Decrease the number of rooms availiable
     room_number = room_number - 1;
     Debug.Log("Room Created! Rooms Remaining: " + room_number);
     
 }
 
 createRoom();
Show more comments

0 Replies

· Add your reply
  • Sort: 

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

23 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

Related Questions

i attach the prefab, but when i hit play, its not the prefab anymore 0 Answers

Prefab Seed-based Procedural Generation (Picture Inclu.) 1 Answer

My randomly generated slope game doesn't seem to work? 0 Answers

How to instantiate prefabs between 2 objects like a path 0 Answers

Call a function in an instanciated prefab has no effect on the prefab 2 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