【发布时间】:2014-01-19 03:00:53
【问题描述】:
谢谢大家!问题是我使用的是 GetComponents 而不是 GetComponent,谢谢!
我对编程很陌生,我正在尝试在 Unity 中制作 2D 游戏。我在网上找到了一个非常好的教程,直到某一点(Tut 可以在这里找到http://pixelnest.io/tutorials/2d-game-unity/shooting-1/)。我遇到了一个错误,不知道为什么。当我尝试在当前脚本中使用另一个脚本时会发生错误(如果这对任何人都有意义的话)。有问题的两个脚本的名称为 ShotScript 和 HealthScript,它们在下面
ShotScript:
using UnityEngine;
using System.Collections;
public class ShotScript : MonoBehaviour {
public int damage = 1;
public bool isEnemyShot = false;
void Start () {
Destroy (gameObject, 20);
}
}
健康脚本:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public int hp = 2;
public bool isEnemy = true;
void OnTriggerEnter2D(Collider2D Collider) {
ShotScript shot = collider.gameObject.GetComponents<ShotScript>();
if (shot != null) {
if (shot.isEnemyShot != isEnemy) {
hp -= shot.damage;
Destroy (shot.gameObject);
if (hp <= 0) {
Destroy(gameObject);
}
}
}
}
}
我得到的错误是:
"Assets/Scripts/HealthScript.cs(13,36):错误 CS0029:无法隐式转换类型 ShotScript[]' toShotScript'"
我有点卡住了,所以如果有人能指出我正确的方向,那就太好了 =)
P.S 我是新手,所以如果你需要任何额外的信息,我会尽力提供它
【问题讨论】:
-
这就是它所说的意思。我假设问题出在
ShotScript shot = collider.gameObject.GetComponents<ShotScript>();GetComponents方法是否返回数组? -
是的,我应该输入 GetComponent =P