x


Array Prefabs

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 =)

more ▼

asked Jul 31 '12 at 05:16 PM

developedAS gravatar image

developedAS
1 1 1 2

don't use an array, use List

Jul 31 '12 at 10:31 PM Fattie
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first
  • first you need declare array like this:
public List<GameObject> PrefabsArray;
  • second, some method to instantiate prefabs and then delete it. wait a little and instantiate again, like this:
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);
    }
}
  • third, you need to start this 'instantiator' like this
void Start()
{
    StartCoroutine(RandomInstantiator());
}
  • fourth, just attach script to any gameobject in scene, attach prefabs and you got it 8)

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);
	}
}
more ▼

answered Jul 31 '12 at 08:24 PM

ScroodgeM gravatar image

ScroodgeM
7.6k 2 6

(comments are locked)
10|3000 characters needed characters left

Thanks Scroodge =) although it had settled, I leave how to fix it .

/* CODE C# */

using UnityEngine; using System.Collections;

public class PowerUps: MonoBehaviour {

private ArrayList powers;
private float time = 300;
private float count;
public GameObject prefSmoke;
public GameObject prefBomba;
public GameObject prefCondor;
public GameObject prefGas;
public GameObject prefIce;
public GameObject prefOtherBall;
public GameObject prefOvni;
public GameObject prefPiranha;
public GameObject prefSmokeBlack;
private GameObject randomPowerUp;

void Start()
{
    powers = new ArrayList();
    powers.Add(prefSmoke);
    powers.Add(prefBomba);
    powers.Add(prefCondor);
    powers.Add(prefGas);
    powers.Add(prefIce);
    powers.Add(prefOvni);
    powers.Add(prefPiranha);
    powers.Add(prefSmokeBlack);
}
void Update()
{

    count++;
    if (count == time)
    {
        float addXPos = Random.Range(-4, 4);
        Vector3 spawnPos = transform.position + new Vector3(addXPos, 3, 0);
        Instantiate(powers[randomPowerUps()] as GameObject, spawnPos, Quaternion.identity);
        count = 0;
    }
}

int randomPowerUps()
{
    int rand = Random.Range(0, powers.Count - 1);
    return rand;
}

}

more ▼

answered Jul 31 '12 at 08:41 PM

developedAS gravatar image

developedAS
1 1 1 2

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)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4138
x838
x224

asked: Jul 31 '12 at 05:16 PM

Seen: 611 times

Last Updated: Jul 31 '12 at 10:31 PM