使用Array.Add将元素添加到数组

时间:2014-07-12 16:34:09

标签: c# arrays unity3d

我是C#的新手,我正在尝试在Unity中制作基本游戏。我正在尝试将子弹gameObject添加到数组中。我研究了如何在C#中向数组中添加元素,并找到了Add方法。但是,当我尝试使用此MonoDevelop时,不会突出显示该方法,就好像该方法不存在而且我收到错误。这是错误消息:

  

Assets / Scripts / SpaceShipController.cs(126,25):错误CS0118:   SpaceShipController.gameManager' is a字段'但是'类型'是   预期

以下是导致错误的代码行:

gameManager.bullets[].Add(bulletObject);

这是我的其余代码。名为SpaceShipController的类在尝试将子弹对象添加到GameManager对象中的数组并且附加了GameManager脚本时会触发错误。最后,BulletBehaviour课程让子弹向前推进。代码由类标记:

SpaceShipController:

using UnityEngine;
using System.Collections;

public class SpaceShipController : MonoBehaviour {

    public GameObject bulletObject;

    public GameManager gameManager;

    //private GameObject[] bullets;

    public float shipSpeed;
    public float bulletSpeed = 1;

    private Vector3 spaceShip;

    private Quaternion spaceShipRotation;

    private Vector3 bulletPosition;

    private int coolDown = 10;

    private bool moveRight = false;
    private bool moveLeft = false;
    private bool fire = false;

    // Use this for initialization
    void Start () {

        spaceShip = transform.position;

        spaceShipRotation = transform.rotation;

        bulletObject.transform.position = bulletPosition;
    }

    // Update is called once per frame
    void Update () {

        coolDown--;

        inputHandler();

        this.transform.position = spaceShip;
    }

    void inputHandler() {

        if (Input.GetKeyDown(KeyCode.RightArrow)) {

            moveRight = true;

        }

        if (Input.GetKeyDown(KeyCode.LeftArrow)) {

            moveLeft = true;

        }

        if (Input.GetKeyUp(KeyCode.RightArrow)) {

            moveRight = false;

        }

        if (Input.GetKeyUp(KeyCode.LeftArrow)) {

            moveLeft = false;

        }

        if (Input.GetKeyDown(KeyCode.Space)) {

            fire = true;
        }

        if (Input.GetKeyUp(KeyCode.Space)) {

            fire = false;
        }

        if (moveRight == true) {

            spaceShip.x += shipSpeed;

        }

        if (moveLeft == true) {

            spaceShip.x -= shipSpeed;

        }


        if (coolDown <= 0) {

            if (fire == true) {

                Fire ();

                coolDown = 10;
            }

        }
    }

    void Fire () {

        for (var i = 0; i < 2; i++) {

            if (i == 0) {

                spaceShip = new Vector3 (transform.position.x + 0.9f, transform.position.y + 0.9f, transform.position.z);
            }

            else if (i == 1) {

                spaceShip = new Vector3 (transform.position.x - 0.9f, transform.position.y + 0.9f, transform.position.z);
            }

            Instantiate(bulletObject, spaceShip, spaceShipRotation);

            bulletObject.AddComponent<BulletBehaviour>();

            gameManager.bullets[].Add(bulletObject);

            spaceShip = this.transform.position;
        }
    }
}

游戏管理:

using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {

    public GameObject[] bullets;

    public Camera cam;

    private Vector2 cameraBounds;

    // Use this for initialization
    void Start () {


        cameraBounds = new Vector2 (cam.orthographicSize * Screen.width/Screen.height, cam.orthographicSize);
    }

    // Update is called once per frame
    void Update () {

        /*for (int i = 0; i < bullets.Length; i++) {

            if (bullets[i].transform.position.y >= cameraBounds.y) {

                Destroy(bullets[i]);

            }
        }*/
    }
}

BulletBehaviour:

using UnityEngine;
using System.Collections;

public class BulletBehaviour : MonoBehaviour {

    public SpaceShipController ship;

    private Vector3 shipPosition;

    // Use this for initialization
    void Start () {

        shipPosition = transform.position;
    }

    // Update is called once per frame
    void Update () {

        shipPosition.y += 1;

        transform.position = shipPosition;
    }
}

一如既往,我们将非常感谢任何帮助。提前感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:4)

阵列是固定大小的。这意味着,一旦它们被初始化了一定长度(例如bullets = new GameObject[10]),它的长度就不能再改变了。

为了将一个项目“添加”到一个数组中,您必须指定您希望该项目在哪个位置。默认情况下,数组的索引是从0开始的。例如,要在第一个位置插入元素:

bullets[0] = myItem;

如果您事先不知道有多少项目,并且想要随意添加/删除项目,则应使用可调整大小的集合,例如List<T>

public List<GameObject> Bullets {get; set;}

您可以像这样使用它:

//initialize with 0 items
Bullets = new List<GameObject>();

//add a new item at the end of the list
Bullets.Add(item);

另请阅读: