• 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 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 anything 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 hierarchy, after the first piece is instantiated:

alt text

As you can see, the 'room_collider' piece is missing, why would this be?

capture.png (9.6 kB)
capture2.png (3.1 kB)
Comment
Add comment · 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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

24 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

Related Questions

inserting random gun into player/enemies hands 3 Answers

How to instantiate and destroy objects with 2d trigger 1 Answer

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

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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges