- Home /
How to dynamically instantiate using a design pattern?
Hi, I'm trying to create some bots dynamically using the abstract factory pattern, but the instantiation process is not occurring. Someone can tell me why, please? I'm getting crazy with this..
public class AvatarsInicializator : MonoBehaviour
{
protected int difficulty=-1;
public int minPlayers=0;
public int maxPlayers=0;
public List<GameObject> listaAvatars;
private List<AbstractPersonagem> listPersonagens;
private int cont;
void Start ()
{
difficulty=MenuController.difficulty;
listPersonagens=new List<AbstractPersonagem>();
criaAvatars ();
cont=0;
}
void criaAvatars()
{
atribuiuActores (minPlayers,maxPlayers);
int quant = 0;
if(listPersonagens.Count>0)
foreach(AbstractPersonagem ac in listPersonagens)
{
switch(ac.GetType ().ToString ())
{
case "NPC":
quant = UnityEngine.Random.Range(1, 5);
if(quant==1) ac.criaRota1 ();
if(quant==2) ac.criaRota2 ();
if(quant==3) ac.criaRota3 ();
if(quant==4) ac.criaRota4 ();
break;
case "Predador":
quant = UnityEngine.Random.Range(1, 4);
if(quant==1) ac.criaRota1 ();
if(quant==3) ac.criaRota3 ();
if(quant==4) ac.criaRota4 ();
break;
case "Presa":
quant = UnityEngine.Random.Range(1, 3);
if(quant==1) ac.criaRota1 ();
if(quant==4) ac.criaRota4 ();
break;
}
int aux = UnityEngine.Random.Range(1, listaAvatars.Count);
GameObject gmO = Instantiate (listaAvatars[aux],new Vector3(1,1,1),Quaternion.Euler(0,0,0)) as GameObject;
gmO.AddComponent (ac.RoTa.ToString ());
switch(ac.roTa.GetType().Name)
{
case "at3parnav": gmO.AddComponent("at3parnav"); break;
case "bt3parnav": gmO.AddComponent("bt3parnav"); break;
case "ct3parnav": gmO.AddComponent("ct3parnav"); break;
case "dt3parnav": gmO.AddComponent("dt3parnav"); break;
}
gmO.name=ac.GetType().ToString ();
}
}
public void atribuiuActores(int min,int max)
{
int quant = UnityEngine.Random.Range(min, max), i=0;
listPersonagens.Add (new Presa());
do
{
if (i == 0)
{
int cont = UnityEngine.Random.Range(3, 5);
for (i = 0; i < cont; i++)
listPersonagens.Add (new Predador());
}
else
listPersonagens.Add (new NPC());
} while (listPersonagens.Count != quant);
}
}
I did a quick read and could not find a pathway where the Instantiate() would not be called. I was a bit puzzled by why...
int aux = UnityEngine.Random.Range(1, listaAvatars.Count);
...was 1 ins$$anonymous$$d of 0, but since attribuiActores will produce a $$anonymous$$imum of 4 entries, this is not causing you any problems.
Have you checked into the following:
The object really didn't get created (i.e. they are not in the hierarchy just out of camera view, or disabled in some way?
That Start() is being called (i.e. the script is attached and the game object is active).
The listPersonagens.Count is > 0 on line 23
That Instantiate() call is being made on line 49
And that there are no errors in the Console window
i was just wondering is you native language spanish, or what is that? cheers!
listAvatars is a public list with 6 gameObjects which I define, so there isn't any problem with that line. I have been testing the instantiate method with a deter$$anonymous$$ate gameObject in some other lines of the criaAvatars method and it works but like this in line 49 don't works. Appears a OutOf$$anonymous$$emoryException but I don't understand why :S I'm portuguese, but I don't see the relevance of this -.-
Your answer
Follow this Question
Related Questions
Change value of a projectile that is instantiated in Awake and pooled 1 Answer
Instantiate Prefab Problem 1 Answer
Instantiate object 1 Answer
Checking if object intersects? 1 Answer
How to instantiate a Prefab ? 2 Answers