Hi, I am designing a game where certain prefabs fill the world, on user input they change again.
I play to have these stored in arrays. what would be the best way to go about doing this? How complex would this be technically?
So basically procedurally/random generated map?
Quite easy, for example,
have list of wall prefabs, select 1 of them randomly, then instantiate that one…and repeat.
what type of game that would be? and is that 2D or 3D level?
nice example plugin: https://www.assetstore.unity3d.com/en/#!/content/15682/assetkit/2881
You could also keep the prefabs in a folder named “Resources” and then use the Resources.Load function to gather each one of them from the database (Unity - Scripting API: Resources.Load )
perhaps you have already thought of that.
Could you be a bit more specific on what you want to achieve / do? Why the arrays, for example? You want to call objects by index?
it would be a 3d level. just being able to change the entire room on user imput thats all im really trying to do
I just thought the arrays would be the easiest way to organize the prefabs. I am organizing them based on mood, meaning like color or look. just being able to spawn a random combination of them at first. then another with user input
I meant like I have 1 room, and i want to change all the assets and things in the room with just like a click or some form of user input.
Imho Arrays sound fine, as long as your content is kind of “fixed” or predetermined (the “content” from which you will make your choices).
And indeed, using them might be quite convenient for easy access.
How are you planning to “populate” those arrays though?
Will you do it by scripting, or will you make public arrays and then drag n drop the prefabs into them?
(this is something I would rather avoid, in case there are lots of prefabs)…
Yeah the prefabs will already be made and set.
At max i think there will be around 40 or so prefabs.
What do you think would be the best way to implement this?
I generally just want the most organized way.
One thing that I have tried in the past (and worked fine for me) is this:
-I made an object in the scene, called PrefabsHolder
-I made a script on that object, called PrefabsHolderScript
-in that script I put a public Array of GameObjects (in your case size would be 40)
public GameObject[ ] prefabsArray;
-Then I dragged - dropped all of my prefabs on to specific places in that array in the editor.
If you do that, then you may reference this array from anywhere like that:
(let’s say from another script)
GameObject ob1 = GameObject.Find(“PrefabsHolder”).GetComponent().prefabsArray[0];
//prefabsArray[0] returns the first object, replace 0 with a random integer from 0 to 39 to get a random object…
instantiate(ob1, transform.position, quaternion.identity);
and so on…
not sure if this is the “best” way, or even a “good” way, to be honest. All I can say is that it has worked for me, in cases that I have a few prefabs (40 is not that many) and I want to do the job quite quickly.
I think this can help you keep things organized, though, because you have a separate object that holds your prefabs and you can see them in the editor.
Also there might be some minor spelling errors in the code above, because I just typed it here…
Hope it helps
It works great thanks.
No problem, glad to have helped ![]()
By the way, if you come across some other technique / approach on the way it would be nice to post it here