使用Array. add向数组中添加元素
本文关键字:添加 元素 数组 Array add 使用 | 更新日期: 2023-09-27 18:12:50
我是c#的新手,我试图在Unity中制作一个基本的游戏。我试图将子弹gameObject添加到数组中。我研究了如何在c#中向数组添加元素,并找到了add方法。然而,当我尝试使用这个MonoDevelop不突出显示方法,如果方法不存在,我得到一个错误。下面是错误信息:
Assets/Scripts/SpaceShipController.cs(126,25): error 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;
}
}
}
GameManager:
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;
}
}
如往常一样,任何帮助都将不胜感激。
数组是固定大小的。这意味着,一旦它们被初始化为一定的长度(例如,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);
请参阅:
- <
- 阵列教程/gh>
-
List<T>
类 < - c#集合/gh>