MarkHughes88
MarkHughes88

Reputation: 669

Instantiating from an array in unity

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

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

edit:

I've been playing with this and this is what I have so far:

using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour {

    public GameObject[] asteroids;
    public Vector3 spawnValues;
    public int asteroidCount;
    public float spawnWait;
    public float startWait;
    public float waveWait;

    void Start () {
        asteroids = GameObject.FindGameObjectsWithTag("Asteroid");
        StartCoroutine (spawnWaves ());
    }

    IEnumerator spawnWaves () {

        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 (asteroids[i], spawnPosition, spawnRotation);
                yield return new WaitForSeconds (spawnWait);
            }
        }

Upvotes: 1

Views: 2212

Answers (3)

Hernantas
Hernantas

Reputation: 341

That's not the correct way of instantiating game objects. Instead, try this one:

Instantiate (Asteroids[i], spawnPosition, spawnRotation);

The error is that the first parameter is a game object, but in your code you pass a float value. Also move your new GameObject[asteroids] code inside the contructor or to the Start() method, or try using a constant/static int value instead.

Upvotes: 3

Hamza Hasan
Hamza Hasan

Reputation: 1398

You cannot initialize array with normal variable in that manner. Either you can do

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

or

int asteroids = 2;     
GameObject[] Asteroids;

void Start()
{
 Asteroids = new GameObject[asteroids];
}

Upvotes: 0

Evgeni Dimitrov
Evgeni Dimitrov

Reputation: 22506

You cannot use one instance variable to initialize another instance variable, because the compiler can't guarantee the order of initialization.

You can do it in the constructor:

public class GameController : MonoBehaviour {

    int asteroids;     
    GameObject[] Asteroids;

    public GameController()
    {
        asteroids = 2;
        Asteroids = new GameObject[asteroids]
    }
...

Or as cubrr said in the comment asteroids can be a constant.

Upvotes: 0

Related Questions