- Home /
Error CS0120
Hello.
A few days ago, I set myself a goal: to create a 3D game. For that, I started learning how to use Unity and C#. Everything went more or less smoothly, with a few bumps here and there. However, yesterday I ran into a metaphorical wall that I've yet to get through.
The problem is the following: I created a script with a class holding several variables and an array to associate them. Then I created another script, which managed the player's health, that needed to access one of those variables. However, when I tried to do that, I got the error described in the title. I researched for a whole day, trying to solve the problem, with no luck. I tried using GetComponent, but there was no difference. Then I tried making everything in question static. The error in question went away, but then I got not one, but two different errors: CS0709 and CS0713.
Here's the code I used, for the first script:
public class Pokemon : MonoBehaviour
{
...
public int health;
...
void Start () {
Pokemon[] Players = new Pokemon[20];
...
Players [0].health = 2000;
And then, on the other script:
public class Health_Bar : Pokemon
{
private int mHealth;
private int cHealth;
...
void Start ()
{
mHealth = Pokemon.Players [0].health;
cHealth = Pokemon.Players [0].health;
...
What am I doing wrong? How do I fix this? Please help me, I'm kind of losing my motivation here :(
When posting about errors, please include the entire error message. Note that most error messages tell you the line on which the error is located (the first number in parenthesis). If you can't figure out a specific error even when you know the line number, ensure you indicate the specific line to us in your posted code.
You seem to be misunderstanding some basics. The best advice I can offer is to spend time going through Unity tutorials and learning C# in earnest. Here's a great resource for learning C# fundamentals super quick: http://rbwhitaker.wikidot.com/c-sharp-tutorials
The big thing that stands out as wrong is that your health bar script shouldn't inherit from pokemon.
Your answer
Follow this Question
Related Questions
Having script accessing issues 2 Answers
NullReferenceException: Object reference not set to an instance of an object 1 Answer
Tagging object while instating not working. 3 Answers
Change 3D Object by pressing Gui Button 1 Answer
Unity return error message like method not a member of Object class 1 Answer