【发布时间】:2014-07-12 16:34:09
【问题描述】:
我是 C# 新手,正在尝试用 Unity 制作一个基本游戏。我正在尝试将子弹游戏对象添加到数组中。我研究了如何在 C# 中向数组添加元素并找到了 Add 方法。但是,当我尝试使用此 MonoDevelop 时,它并没有突出显示该方法,就好像该方法不存在一样,并且出现错误。这是错误消息:
资产/脚本/SpaceShipController.cs(126,25):错误 CS0118:
SpaceShipController.gameManager' is afield' 但“类型”是 预计
这是导致错误的代码行:
gameManager.bullets[].Add(bulletObject);
这是我的其余代码。名为 SpaceShipController 的类在尝试将子弹对象添加到带有脚本 GameManager 的 GameManager 对象中的数组时触发错误。最后 BulletBehaviour 类只是让子弹向前移动。代码按类标记:
太空船控制器:
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]);
}
}*/
}
}
子弹行为:
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;
}
}
一如既往,我们将不胜感激任何帮助。提前感谢您提供的任何帮助。
【问题讨论】:
-
访问子弹变量时,不需要[]括号。你可以说 gameManager.bullets.Add(bulletObject);
-
@Simon 没错,
[]不需要调用数组上的方法。但更大的问题是,数组没有Add方法。 -
另外,为了将来参考,请提供最短的代码来重新创建问题,而不是一堵文字墙。问题是关于如何将项目添加到数组中——你不需要告诉我们关于统一和所有这些类的信息。请参阅帮助中心:How to create a Minimal, Complete, and Verifiable example
-
好的,谢谢,下次我会尽量记住这一点。