- Home /
Object Pooling
I am trying to make this code works, I have 6 prefab scenarios that I want to use in my game. in the start function I made 3 copies of each prefab, making a total of 18 pooled objects in the game.
when the function spawntile() is called I want that one of this 18 pooled objects that is inactive, turns active and show in the gameview. all other parts is working. just the part:
go = ActiveTiles [RandomPrefabIndex].SetActive (true);
go = ActiveTiles [PrefabIndex].SetActive (true);
dont work, I was using:
go = Instantiate (ActiveTiles[RandomPrefabIndex]) as GameObject;
go = Instantiate (ActiveTiles[PrefabIndex]) as GameObject;
and this works but create a new object, and I need the code active one of 18 inactive objects.
I am receiving this erros
error CS1502: The best overloaded method match for System.Collections.Generic.List<UnityEngine.GameObject>.this[int]' has some invalid arguments error CS1503: Argument #1' cannot convert method group' expression to type int' error CS0029: Cannot implicitly convert type void' to UnityEngine.GameObject'
thanks for the help!
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class TILEMANAGER : MonoBehaviour {
public GameObject[] TileSPrefabs;
private Transform PlayerTransform;
private float SpawnZ = -10.0f;
private float TileLenght = 10.0f;
private float SafeZone = 15.0f;
private int AmountTilesOnScreen = 8;
private int LastPrefabIndex = 0;
private int PooledTiles = 3;
private List<GameObject> ActiveTiles;
// Use this for initialization
void Start () {
ActiveTiles = new List<GameObject> ();
PlayerTransform = GameObject.FindGameObjectWithTag ("Jogador").transform;
foreach (GameObject objectprefab in TileSPrefabs){
for (int i = 0; i < PooledTiles; i++) {
GameObject obj = Instantiate (objectprefab) as GameObject;
obj.SetActive (false);
ActiveTiles.Add (obj);
}
}
for (int i = 0; i < AmountTilesOnScreen; i++) {
if (i < 3)
SpawnTile (0);
else
SpawnTile ();
}
}
// Update is called once per frame
void Update () {
if (PlayerTransform.position.z - SafeZone > (SpawnZ - AmountTilesOnScreen * TileLenght)) {
SpawnTile ();
DeleteTile ();
}
}
void SpawnTile(int PrefabIndex = -1){
GameObject go;
if (PrefabIndex == -1)
// the error is here//
go = ActiveTiles [RandomPrefabIndex].SetActive (true);
else
// and here//
go = ActiveTiles [PrefabIndex].SetActive (true);
go.transform.SetParent (transform);
go.transform.position = Vector3.forward * SpawnZ;
SpawnZ += TileLenght;
ActiveTiles.Add (go);
}
void DeleteTile(){
ActiveTiles [0].SetActive (false);
}
private int RandomPrefabIndex(){
if (ActiveTiles.Count <= 1)
return 0;
int RandomIndex = LastPrefabIndex;
while (RandomIndex == LastPrefabIndex) {
RandomIndex = Random.Range (0, ActiveTiles.Count);
}
LastPrefabIndex = RandomIndex;
return RandomIndex;
}
}
Answer by Landern · Aug 03, 2016 at 03:17 PM
You're calling a method line 47. You need to include the parenthesis.
Wrong:
go = ActiveTiles [RandomPrefabIndex].SetActive (true);
Right:
go = ActiveTiles [RandomPrefabIndex()].SetActive (true);
Your answer
![](https://koobas.hobune.stream/wayback/20220612090044im_/https://answers.unity.com/themes/thub/images/avi.jpg)