【问题标题】:Instantiating from an array in unity从一个数组中统一实例化
【发布时间】:2016-02-24 14:05:30
【问题描述】:

我正在尝试实例化存储在数组中的随机小行星游戏对象。但是,我遇到了一个错误,无法解决。谁能帮忙:

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

编辑:

我一直在玩这个,这就是我到目前为止所拥有的:

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

【问题讨论】:

  • int asteroids = 2; 需要为staticconst,或者你需要将Asteroids = new GameObject[asteroids]; 移动到构造函数中(或Start());

标签: c# arrays random unity3d instantiation


【解决方案1】:

这不是实例化游戏对象的正确方法。相反,试试这个:

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

错误是第一个参数是一个游戏对象,但在你的代码中你传递了一个浮点值。还可以将 new GameObject[asteroids] 代码移到构造函数内或 Start() 方法中,或者尝试使用常量/静态 int 值。

【讨论】:

  • 我已经更新了这个。我没有意识到我没有在实例化命令中声明数组!但是,这似乎仍然不起作用。我的游戏现在并没有在播放时冻结,我想这是一个好的开始,但它没有实例化任何东西。有任何想法吗?我已经在原始帖子中重新发布了更新的代码:)
  • 你确定实例化被调用了吗?因为 asteroidCount 没有设置,所以我假设它的值为 0。
  • 我得到了这个工作。 asteroidCount 是公开的,并在组件编辑器中声明。问题似乎是我没有正确加载数组。我最终还手动将预制件放入数组中,而不是通过游戏对象标签
【解决方案2】:

您不能以这种方式使用普通变量初始化数组。 要么你可以做

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

int asteroids = 2;     
GameObject[] Asteroids;

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

【讨论】:

    【解决方案3】:

    你不能使用一个实例变量来初始化另一个实例变量,因为编译器不能保证初始化的顺序。

    你可以在构造函数中做:

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

    或者正如cubrr在评论中所说,小行星可以是一个常数。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 2019-07-31
    • 2014-09-13
    • 2014-03-16
    • 2017-06-07
    相关资源
    最近更新 更多