dimanche 22 novembre 2015

Instantiating from an array in unity

I'm trying to instantiate a random asteroid gameobject that ive stored within an array. However I am getting an error with this and can't work it out. Can anyone help:

error: Assets/Scripts/GameController.cs(7,49): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `GameController.asteroids'

using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour {

    int asteroids = 2;     
    GameObject[] Asteroids = new GameObject[asteroids];

    public Vector3 spawnValues;
    public int asteroidCount;
    public float spawnWait;
    public float startWait;
    public float waveWait;

    void Start () {

        //call asteroid array variables
        Asteroids [0] = gameObject.tag == "Asteroid01";
        Asteroids [1] = gameObject.tag == "Asteroid02";

        StartCoroutine (spawnWaves ());
    }

    IEnumerator spawnWaves () {

        yield return new WaitForSeconds (startWait);

        while (true) {
            for (int i = 0; i < asteroidCount; i++) {
                Vector3 spawnPosition = new Vector3 (spawnValues.x, Random.Range (-spawnValues.y, spawnValues.y), spawnValues.z);
                Quaternion spawnRotation = Quaternion.identity;

                Instantiate (Random.Range(0,1), spawnPosition, spawnRotation);
                yield return new WaitForSeconds (spawnWait);
            }
        }
    }




Aucun commentaire:

Enregistrer un commentaire