So I began learning Swift, but I discovered that I wanted to use Unity3d because it looked interesting to learn, and for games it looked like the better option than Xcode. This meant that I had to learn a new language, however, so I started learning C#. In my project I have 2 classes.
My first Class is LivesDetract. This class is an edge collider. It catches the falling objects, and decreases the life amount:
public class LivesDetract : MonoBehavior {
public int currentLives; //set to 3 in Unity
private int livesDecrementAmnt = 1;
void OnTriggerEnter2D (Collider2D other) {
currentLives = currentLives - livesDecrementAmnt;
}
}
The second class is named GameController. GameController controls the flow of the game. I originally had LivesDetract as a part of GameController, but for organization purposes, I think it should be in its own class. The problem is that I get the following error when I try to inherit from the class LivesDetract:
Error: "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
}
}
}
I think I have provided enough information, but if more is needed let me know. In Swift I was able to set the function as a variable:
var livesDetract = LivesDetract()
I could then use:
while livesDetract.currentLives > 0 {
}
That doesn't seem to work in C# though.
You are trying to access currentLives without instantiating the object. You need find the object in Unity before making use of it.