i am trying to pool object from a list wherethere are tho kinds of objects 1.spnere and 2.cube.
i want them to instantiate them randomly from a point randomly. I am using code below where there is only one prefab. Dont know what to do ahead.
any help??
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Poolingscript : MonoBehaviour
{
public GameObject bull;
public GameObject bullet;
public int listsize = 50;
public List<GameObject> bullets;
// Use this for initialization
void Start ()
{
createlist ();
}
void createlist()
{
for (int i=0; i<listsize; i++)
{
bull = Instantiate(bullet)as GameObject;
bull.SetActive(false);
bullets.Add(bull);
}
}
GameObject temp;
public void Update()
{
if(Input.GetButtonDown("Fire1"))
{
Debug.Log(Input.mousePosition);
temp=bullets.First();
bullets.Remove(temp);
temp.transform.position = transform.position;
temp.SetActive(true);
}
}
public void addmethod(GameObject g)
{
g.SetActive (false);
bullets.Add (g);
}
}
after working for 6 hours, finally i’ve come with the following answer. but now i want to fall these objects automatically one after one. in forst, the rate of falling onject is slow. as time passes the rate of falling objects keeps increasing. But i’m wondering how is it possible??
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Poolingscript2 : MonoBehaviour {
public GameObject[] allprefabs;
public List<GameObject> bullets ;
public int listsize;
GameObject go;
// Use this for initialization
void Start ()
{
createlist ();
}
void createlist()
{
allprefabs = Resources.LoadAll<GameObject> ("Prefab");
for (int i=0; i<listsize; i++)
{
foreach (GameObject allprifab in allprefabs)
{
go = Instantiate(allprifab)as GameObject;
go.SetActive(false);
bullets.Add (go);
}
}
}
GameObject temp;
void fallobject()
{
int randm = Random.Range(0,listsize);
temp = bullets.ElementAt(randm);
bullets.Remove(temp);
temp.transform.position = transform.position;
temp.SetActive(true);
Debug.Log ("removed element no: " +randm);
}
public void Update()
{
}
public void addtolist(GameObject g)
{
g.SetActive (true);
bullets.Add (g);
}
}