|
Hello! , Excuse my English, I am developing a game with powerups and I have many prefabs want to do an array of these prefabs and randomly draw one of them from time to time, could you please help me =)
(comments are locked)
|
public List<GameObject> PrefabsArray;
IEnumerator RandomInstantiator()
{
while(true)
{
GameObject instance = Instantiate(PrefabsArray[UnityEngine.Random.Range(0, PrefabsArray.Count)]) as GameObject;
yield return new WaitForSeconds(5f);
Destroy(instance);
yield return new WaitForSeconds(1f);
}
}
void Start()
{
StartCoroutine(RandomInstantiator());
}
note: C# finally/* CODE C# */
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PowerUps : MonoBehaviour
{
public List powers;
private float time = 5f; // in seconds
void Start()
{
StartCoroutine(RandomInstantiator());
}
IEnumerator RandomInstantiator()
{
while (true)
{
float addXPos = Random.Range(-4, 4);
Vector3 spawnPos = transform.position + new Vector3(addXPos, 3, 0);
Instantiate(powers[randomPowerUps()], spawnPos, Quaternion.identity);
yield return new WaitForSeconds(time);
}
}
int randomPowerUps()
{
return UnityEngine.Random.Range(0, powers.Count);
}
}
(comments are locked)
|
|
Thanks Scroodge =) although it had settled, I leave how to fix it . /* CODE C# */ using UnityEngine; using System.Collections; public class PowerUps: MonoBehaviour { } i'd edit y answer with optimized a little script in your post 8)
Jul 31 '12 at 08:55 PM
ScroodgeM
(comments are locked)
|

don't use an array, use List