-1

I'm Currently creating a tycoon game, and i've got a problem on how to spawn random objects for the customer. this is not the first time, i learned Random Spawning once, 2 ago from other genre, but in Unity 5.3, if i'm not wrong. so i have to start from scratch again. so i tried a tutorial video, and here's how the coding goes, with a little modification:

public Text TimerText;
public float LevelTimeLimit;
private bool FullTime = false;

public Text TimerText;
public float LevelTimeLimit;
private bool FullTime = false;

public GameObject[] ObjPrefabs;


[System.Serializable]
public class myPool{
    public string tag;
    public GameObject Customer;
    public int ServiceTime;
}

public List<GameObject> CustomerPool;
public Dictionary<string, Queue<GameObject>> PoolDictionary;

public GameObject GetCust(string type)
{
    for (int i = 0; i < ObjPrefabs.Length; i++)
    {

        GameObject newObject = (GameObject)Instantiate(ObjPrefabs[i]);
        newObject.SetActive(false);
        CustomerPool.Add(newObject);

    }

    return null;
}

my problem is it won't spawn random customer. anybody know what did i miss?

Johnny
  • 8,939
  • 2
  • 28
  • 33
  • This code, as is, creates every customer prefab once and stores it in your object pool. If I am reading your question right you want x ammount of random customers in your pool. Is that correct? – Nicolas Mar 21 '19 at 08:54
  • Not using [System.Random](https://learn.microsoft.com/en-us/dotnet/api/system.random?view=netframework-4.7.2)? – Neijwiert Mar 21 '19 at 08:54
  • There is no random in your code – Cid Mar 21 '19 at 08:54
  • @Neijwiert Unity 3D has its own [Random](https://docs.unity3d.com/ScriptReference/Random.html) – Cid Mar 21 '19 at 08:56
  • 1
    Well, you're instantiating all your objects, then hiding them first, and then, you are returning NULL 100% of the time in a GameObject function, which is highly questionable, especially with no context or idea of why you chose this. You're also not using any type of 'random' anywhere, meaning you will always have the same result with this code. – Antry Mar 21 '19 at 08:56
  • I guess what you want to do is on line `GameObject newObject = (GameObject)Instantiate(ObjPrefabs[i]);` change the `i` to something like `Random.Range(0, ObjPrefabs.length)` to get random prefabs from array. – Antry Mar 21 '19 at 09:03
  • For starters why have you got a function called `GetCust` with a return type of `GameObject` that never returns anything? I'm thinking that would be better off called `BuildCustPool` without a return type set using `void` and then make a separate function that would return a random `GameObject` with `return CustomerPool[Random.Range(0, CustomerPool.Count)]` as the method within it to return a random `GameObject` – akaBase Mar 21 '19 at 10:27

1 Answers1

0
for (int i = 0; i < ObjPrefabs.Length; i++)
{

    GameObject newObject = (GameObject)Instantiate(ObjPrefabs[i]);
    newObject.SetActive(false);
    CustomerPool.Add(newObject);

}

this code spawn each of the prefabs in the list. if you want to spawn random prefab from the list try to replace it with this code.

GameObject newObject = (GameObject)Instantiate(ObjPrefabs[Random.Range(0, ObjPrefabs.Length)]);

random.range(0, ObjPrefabs.Length) - pick random number from 0 to ObjPrefabs.Length.

and take from ObjPrefabs the element in that position.