i m currently working on a script where i want to drop random objects from a list of prefabs. i am using pool objects for this. The problem is as follow: i want these objects one by one when game starts. The rate of falling objects should be slow when game starts. But as time passes, the objects shold fall with increasing more speed. How to do that?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Poolingscript2 : MonoBehaviour {
public GameObject[] allprefabs; //array of prefab
public List<GameObject> bullets ;
public int listsize;
GameObject go;
// Use this for initialization
void Start ()
{
createlist ();
}
void createlist() //creates list of gameobjects from array of prefabs
{
allprefabs = Resources.LoadAll<GameObject> ("Prefab"); //reload prefabs in array
for (int i=0; i<listsize; i++)
{
foreach (GameObject allprifab in allprefabs)
{
go = Instantiate(allprifab)as GameObject; //make object from aray
go.SetActive(false);
bullets.Add (go); //add to list
}
}
}
GameObject temp;
void fallobject()
{
int randm = Random.Range(0,listsize); //select random number
temp = bullets.ElementAt(randm); // assign number and select random object
bullets.Remove(temp); // remove that random object
temp.transform.position = transform.position;
temp.SetActive(true); //activate, so it falls down
Debug.Log ("removed element no: " +randm);
}
public void Update()
{
}
public void addtolist(GameObject g) //function for adding fallen object back to list
{
g.SetActive (true);
bullets.Add (g);
}
}