如何在 Unity3d MonoDevelop 中使用继承?
How do I use inheritance in Unity3d MonoDevelop?
所以我开始学习 Swift,但我发现我想使用 Unity3d,因为它看起来很有趣,而且对于游戏来说,它看起来比 Xcode 更好。然而,这意味着我必须学习一门新语言,所以我开始学习 C#。在我的项目中,我有 2 classes。
我的第一个 Class 是 LivesDetract。这个 class 是一个边缘碰撞器。接住掉落物,减少生命值:
public class LivesDetract : MonoBehavior {
public int currentLives; //set to 3 in Unity
private int livesDecrementAmnt = 1;
void OnTriggerEnter2D (Collider2D other) {
currentLives = currentLives - livesDecrementAmnt;
}
}
第二个 class 名为 GameController。 GameController 控制游戏的流程。我最初将 LivesDetract 作为 GameController 的一部分,但出于组织目的,我认为它应该在自己的 class 中。问题是当我尝试从 class LivesDetract:
继承时出现以下错误
错误:
"An object reference is required for the non-static field, method, or property 'LivesDetract.currentLives'"
public class GameController : MonoBehavior {
IEnumeratorSpawn () {
while(LivesDetract.currentLives > 0) { // This line is where the error occurs
//Game Actions Occur
}
}
}
我想我已经提供了足够的信息,但如果需要更多信息,请告诉我。
在 Swift 中,我能够将函数设置为变量:
var livesDetract = LivesDetract()
然后我可以使用:
while livesDetract.currentLives > 0 {
}
虽然这在 C# 中似乎不起作用。
您正试图在不实例化对象的情况下访问 currentLives。使用前需要先在Unity中找到对象
所以我开始学习 Swift,但我发现我想使用 Unity3d,因为它看起来很有趣,而且对于游戏来说,它看起来比 Xcode 更好。然而,这意味着我必须学习一门新语言,所以我开始学习 C#。在我的项目中,我有 2 classes。
我的第一个 Class 是 LivesDetract。这个 class 是一个边缘碰撞器。接住掉落物,减少生命值:
public class LivesDetract : MonoBehavior {
public int currentLives; //set to 3 in Unity
private int livesDecrementAmnt = 1;
void OnTriggerEnter2D (Collider2D other) {
currentLives = currentLives - livesDecrementAmnt;
}
}
第二个 class 名为 GameController。 GameController 控制游戏的流程。我最初将 LivesDetract 作为 GameController 的一部分,但出于组织目的,我认为它应该在自己的 class 中。问题是当我尝试从 class LivesDetract:
继承时出现以下错误错误: "An object reference is required for the non-static field, method, or property 'LivesDetract.currentLives'"
public class GameController : MonoBehavior {
IEnumeratorSpawn () {
while(LivesDetract.currentLives > 0) { // This line is where the error occurs
//Game Actions Occur
}
}
}
我想我已经提供了足够的信息,但如果需要更多信息,请告诉我。 在 Swift 中,我能够将函数设置为变量:
var livesDetract = LivesDetract()
然后我可以使用:
while livesDetract.currentLives > 0 {
}
虽然这在 C# 中似乎不起作用。
您正试图在不实例化对象的情况下访问 currentLives。使用前需要先在Unity中找到对象