choose 1 of multiple objects to spawn, respawn once its dead.

im trying to create a mario kart style crate that spawns in the middle of the race track.

  • at each spawn point i want to choose between 4 prefabs and spawn it.
  • once its been hit wait 3 seconds to respawn another random box.

this is the script i have

var BulletCratePrefab : Transform;
var ExploBulletCratePrefab : Transform;
var HitPointCratePrefab : Transform;
var NosCratePrefab : Transform;

var CrateInstance = gameObject;

private var spawnPoint : Vector3;

function Update()
{

    while(true)
    {
        Spawn();

        while(CrateInstance) //wait until the current crate on spawn point is dead.
        {
            return null;
        }
    }
}

function Spawn ()
{
    var index = Random.Range(0 , 3);
    spawnPoint = this.transform.position;

    // choose which prefab to put on track
    if(index == 0)
    {
        CrateInstance = (gameObject)Instantiate (BulletCratePrefab, spawnPoint, this.transform.rotation);
    }
    else if(index == 1)
    {
        CrateInstance = (gameObject)Instantiate (ExploBulletCratePrefab, spawnPoint, this.transform.rotation);
    }   
    else if(index == 2)
    {
        CrateInstance = (gameObject)Instantiate (HitPointCratePrefab, spawnPoint, this.transform.rotation);
    }
    else if(index == 3)
    {
        CrateInstance = (gameObject)Instantiate (NosCratePrefab, spawnPoint, this.transform.rotation);
    }   
}

the only referrence i had was for c# so im not sure if i can do it the same.

Edit: this is the c# script i have. it selects 1 of xAmount and spawns a target, once its dead it spawns again.

using UnityEngine;
using System.Collections;

public class Spawner : MonoBehaviour 
{
    public GameObject TargetPrefab; // Set this in inspector to the target prefab.
    public int RandTimeMin = 3;  // Minimum amount of time before spawning enemy.
    public int RandTimeMax = 10;  // Maximum amount of time before spawning enemy.
    GameObject targetInstance;
    Transform[] spawnpoints;    
    public int SpawnNumber = 10;
    int Spawned = 0;
    int lastIndex =0;
    IEnumerator Start()
    {
        spawnpoints = GetComponentsInChildren<Transform>();

        while (true && Spawned < SpawnNumber)
        {
            yield return new WaitForSeconds(Random.Range(RandTimeMin, RandTimeMax)); // wait X for next target.
            Spawn();
            while (targetInstance) // Wait for target to die.
            {
                yield return null;
            }
            yield return new WaitForSeconds(Random.Range(RandTimeMin, RandTimeMax)); // wait X for next target.
        }

        yield return new WaitForSeconds(6);
        Application.LoadLevel(0);
    }

    void Spawn()
    {
         int index = 0;
         Spawned++;

         while(lastIndex == index )
         {
             index = Random.Range(0, spawnpoints.Length - 1);
         }
         var spawnpoint = spawnpoints[index];
         targetInstance = (GameObject)Instantiate(TargetPrefab, spawnpoint.position, spawnpoint.rotation);
    }
}

instead of creating a list of spawnpoints, i thought it would be easier to handle each spawnpoint individually so if its crate is destroyed it automatically deals with it. as you can see i havent translated the c# in to Js and my idea very well.

gameObject CrateInstance; should be var CrateInstance : gameObject;

thanks i couldnt remember how to do it lol

3 Answers

3

Well...

I can't really read that script in the Update function, looks like an infinite loop in my opinion... So the game shouldn't even run...

Well.. What I'd do, because this is all I can do to really help you, is say how I'd do it..., so, what I'd do is:

Make an array of availible boxes that can be used...

Now, it's Mario Kart style, so every spot on the map that you want on, their should be one.

So, you put in "place holders" these can be with empty game objects, or with an actual image that isn't rendered (So you can actively see it in the editor if you sway that way =P), and so you loop through with a for:each, and you'd grab a random number, and with the number, you'd just spawn all the items in the function awake.

Now if someone goes over and takes one of the boxes... You have in the update function, to keep track of how many was TO START, so now, you have let's say 30 boxes... So, if the number of of current objects (You can loop this in the Update), is less than the start, then you find where it is (Maybe use a List?), and add it there...

This should work for everything I believe...

Hope this helps a little, even though it's not directly related to you're code...

see updated question

i wrote the c# script for a different game. the script is going to be on a spawnpoint prefab, so i can position it on the track where i want. each point will select one of the 4 crates, and spawn it. once its destroys it does the same.

Just use an array.

var powerups : GameObject[];
var powerup  : GameObject;
var minDelay : float = 3;
var maxDelay : float = 5;

Spawn();

function Update() {
    if (!powerup && !IsInvoking("Spawn"))
        Invoke("Spawn", Random.Range(minDelay, maxDelay))
}

function Spawn() {
    var prefab = powerups[Random.Range(0, powerups.Length)];
    powerup = Instantiate(prefab, transform.position, transform.rotation);
}

You're not supposed to set powerup in the inspector. I just kept it public so you can easier see what powerup is active (although you should probably be able to see it in the scene easily). It should probably be private or @HideInInspector.

i went back to my c# script and realized in my game it doesn't matter its c# so changed it.

this works perfectly.

using UnityEngine;
using System.Collections;

public class PowerUp_Script : MonoBehaviour 
{
    public GameObject NormBulletCratePrefab; // Set this in inspector to the target prefab.
    public GameObject ExploBulletCratePrefab; // Set this in inspector to the target prefab.
    public GameObject HPCratePrefab; // Set this in inspector to the target prefab.
    public GameObject NosCratePrefab; // Set this in inspector to the target prefab.

    public int RandTimeMin = 3;  // Minimum amount of time before spawning enemy.
    public int RandTimeMax = 5;  // Maximum amount of time before spawning enemy.
    GameObject CrateInstance;

    IEnumerator Start()
    {
        while (true)
        {
            Spawn();
            while (CrateInstance) // Wait for target to die.
            {
                yield return null;
            }
            yield return new WaitForSeconds(Random.Range(RandTimeMin, RandTimeMax)); // wait X for next target.
        }
    }

    void Spawn()
    {
         int index = Random.Range(0, 3);

         //select the box to spawn
         if(index == 0)
         {
            CrateInstance = (GameObject)Instantiate(NormBulletCratePrefab, this.transform.position, this.transform.rotation);
         }
         if(index == 1)
         {
            CrateInstance = (GameObject)Instantiate(ExploBulletCratePrefab, this.transform.position, this.transform.rotation);
         }      
         if(index == 2)
         {
            CrateInstance = (GameObject)Instantiate(HPCratePrefab, this.transform.position, this.transform.rotation);
         }            
         if(index == 3)
         {
            CrateInstance = (GameObject)Instantiate(NosCratePrefab, this.transform.position, this.transform.rotation);
         }        
    }
}