謝謝大家!問題是我使用GetComponents而不是GetComponent,謝謝!C#(Unity 4.3)不能隱式地將類型`ShotScript []'轉換爲`ShotScript'
我對編程相當陌生,我試圖在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);
}
}
HealthScript:
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);
}
}
}
}
}
我得到的錯誤是: 「資產/腳本/ HealthScript .cs(13,36):錯誤CS0029:無法隱式轉換類型ShotScript[]' to
ShotScript'「
我比較卡住,所以我任何人都可以指向我正確的方向,這將是偉大=)
PS我是新來這個問問題的事情,所以如果你需要任何額外的信息,我會盡我所能提供它
它意味着它說什麼。我假設問題出現在'ShotScript shot = collider.gameObject.GetComponents();''GetComponents'方法是否返回一個數組? –
是的,我應該輸入GetComponent = P – Swindy