- Home /
creating and using list of prefabs
hello, I have a scripts that create some new prefabs and store them in AssetDatabase, and also in a list of gameObjects. I have two questions: 1. I want to run this scripts only one time, in the initialization of the game. how can I do it? 2. I have a script that is called later during the game, and in this scripts I want to use the prefabs from the first scripts. (I want to take the list of all the prefabs created).
Can someone help - how can I do it?
Thanks
Answer by Madrig88 · Sep 05, 2016 at 06:12 PM
One way you can attempt this is by using a global variable in your Database script, and then forwarding a variable from another script. Here is how I would do this...
AssetDBScript:
// Add global variables to be modified elsewhere.
static var prefab1 : GameObject;
static var prefab2 : GameObject;
//etc....
Then when you are ready to start the game, use this script to modify the global variable in the AssetDBScript.
#pragma strict
// Prefab variable names
public var prefabName1 : GameObject;
public var prefabName2 : GameObject;
// etc...
function Start () {
// On start, apply variable.
AssetDBScript.prefab1 = prefabName1;
AssetDBScript.prefab2 = prefabName2;
}
When you are done with the script, you can add the prefab to the variable using the inspector.
Thanks! If I use this way, I didnt understand - how do I use the list I created in the scripts? I have around 80 prefabs so I must use the scripts. #pragma strict
// Prefab variable names
public List<GameObject> listOfPrefabs;
// etc...
function Start () {
// On start, apply variable.
//here , I need to use the script. let's call it generatePrefabs.
//how it is done? maybe somethong like:
AssetDBScript.listOfPrefabs = prefabsGenerator.getList()
}
Answer by ItaloSouza · Sep 05, 2016 at 07:54 PM
Alternatively, you should look for the "Singleton Design pattern".
Thanks, but the same question as to the other reply remains - even if I create the singleton, I dont understand how to use it later, during the game?