- Home /
Instantiating prefabs to every object in an array?
Hey all,
I'm working on an office level, and I've opted to dynamically generate all of the decor for the cubicles. I've managed to get this working on a single cube, but now I'd like to expand it out to the rest using the same script. Here's what I've got so far:
var computerLocation : Transform;
var computerPrefabs : GameObject[];
var plantLocation : Transform;
var plantPrefabs : GameObject[];
function Start() {
GenerateDecor();
}
function GenerateDecor () {
Instantiate (computerPrefabs[Random.Range(0, computerPrefabs.Length)],computerLocation.transform.position, computerLocation.transform.rotation);
Instantiate (plantPrefabs[Random.Range(0, plantPrefabs.Length)],plantLocation.transform.position,Quaternion.identity);
}
Every cube will have the same collection of object types, so it seems I should just make my location variables arrays... But that's where I become lost. I haven't worked with arrays much, so I'm not sure how to tell the script to add a random prefab to each location in the proposed array.
If someone could point me in the right direction, I'd greatly appreciate it!
I think you are wanting to create a base class that defines what a 'cubicle' is, and also know how to use the default constructor to add things to a List of ; this might help
http://unity3d.com/learn/tutorials/modules/intermediate/scripting/inheritance
http://unity3d.com/learn/tutorials/modules/intermediate/scripting/lists-and-dictionaries
Thanks for the reply! In regards to your first comment regarding Random.Range, I've seen all of the prefabs from my array appear during testing.. It's never been my experience that Random.Range wouldn't select the last object.
I'm not sure I understand what the use of creating a class would be in this situation. While I haven't used classes for anything before in Unity, the video you linked to explained it quite well. Could you explain why you think it would be worthwhile here?
As a side note I realized that I didn't mention in my first post that the locations are simply game empties. It's probably obvious and unimportant, but I figured I should mention it anyway!
Oops, sorry I did interpret that wrong while it's true that RandomRange(0,3) would never return 3 (int returns in inclusive on max) the zero-filled List would return 0,1,2 which is correct. You can verify the max-never-returned bit here
http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html
Re: the Class, it might be overboard to create a class Cubicle defining all the various properties; since you were creating an office it seemed like you might benefit from having a Cubicle class to add all the various things that might grow to be used for.
Answer by OnyxRook · Jan 19, 2014 at 10:26 PM
Did a bit more research and discussed my problem with a couple of other devs, and the now very obvious answer made its appearance: a for loop.
Derp! I feel silly now. Documenting in case someone else has the same quandary at some point:
function GenerateDecor(){
for(var i : int = 0; i < computerLocation.length; i++)
{
Instantiate (computerPrefabs[Random.Range(0,computerPrefabs.Length)], computerLocation[i].position,computerLocation[i].rotation);
}
}
Changed my location variables to arrays as I had thought I should, and that's it. I'll just go study more javascript now...
Good deal. Oi, I thought you wanted to capture/add the Instantiated GameObjects into a List (i.e Cubicle)
Haha, no, nothing so complex. I think half of my problem is that I kind of suck at explaining my scripting problems. Working on improving that! Thanks for trying to help out though!