- Home /
Question by
cassiojs · May 11, 2020 at 08:17 AM ·
randommethodpoolingobject poolrandomspawning
How to add randomness in the following code snippet?
How to add randomness in the following line of code (marked with //):
public GameObject GetPooledObject()
{
for (int i = 0; i < pooledObjects.Count; i++)
{
if (!pooledObjects[i].activeInHierarchy)
{
return pooledObjects[i];
}
}
if (shouldExpand)
{
GameObject obj = (GameObject)Instantiate(easySegments[Random.Range(0, easySegments.Count)]);
obj.SetActive(false);
pooledObjects.Add(obj);
return obj;
}
return null;
}
private void SpawnLevelPart()
{
distanceBetween = Random.Range(distanceBetweenMin, distanceBetweenMax);
currentSpawnZ = PlatformDestroyer.comprimento;
transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + currentSpawnZ + distanceBetween);
GameObject newPlatform;
newPlatform = GetPooledObject(); // <--------- here
// like this ---> newPlatform = difficultyLevelPartList[Random.Range(0, difficultyLevelPartList.Count)].GetPooledObject();
newPlatform.transform.position = transform.position;
newPlatform.transform.rotation = transform.rotation;
newPlatform.SetActive(true);
}
When I call this method from another script, it works perfectly, but when it's in the same script, I can't randomize.
I hope you understood my question. Thank you.
Comment