Christian Tucker
Christian Tucker

Reputation: 49

How do I call an variable from another class in C#

I come from Java and I'm picking up C# scripting, I've had this issue for around two days now looking for solutions, I've tried setting the class to an instance and everything. This is for a miniature game project that I am working on with some friends.

Either way, I have StatHandler.cs which handles all of our statpoints... then I have HealthManager.cs which is supposed to handle all of the Health Related stuff.

The problem is, I can't for the life of me figure out how to call the variables such as

public int stamina, strength, agility, dexterity, wisdom;

from StatHandler.cs

I know that in Java it would be as easy as

maxHealth = StatHandler.stamina * 10;

Although, you cannot do that with C#, when creating an instance the code looks like this

maxHealth = StatHandler.instance.stamina * 10;

which provides me with the error

NullReferenceException: Object reference not set to an instance of an object

I've also tried inherriting, by doing this

public class HealthHandler : StatHandler {

but it sets all the values to 0 in the HealthHandler class, it doesn't read anything.


I really just need to figure out how to pull the variable from the other C# files, because this is slowing me way down.

Upvotes: 3

Views: 15206

Answers (4)

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23675

You have two ways to go here.

Full Static Class

public static class StatHandler
{
    public static Int32 Stamina = 10;
    public static Int32 Strength = 5;
}

And then:

maxHealth = StatHandler.Stamina * 10; // get the value and use it
StatHandler.Stamina = 19; // change the value

Singleton Instance

public class StatHandler
{
    public static StatHandler Instance;

    public Int32 Stamina = 10;
    public Int32 Strength = 5;

    // this is called only the first time you invoke the class
    static StatHandler()
    {
        m_Instance = new Instance(); // Replace Instance with type of instance
    }
}

And then:

maxHealth = StatHandler.Instance.Stamina * 10; // get the value and use it
StatHandler.Instance.Stamina = 19; // change the value

// replace the current instance:
StatHandler.Instance = new StatHandler();
StatHandler.Instance.Stamina = 19; // change the value

I think the first one is always the best choice, same result, less complexity.

Upvotes: 0

Tilak
Tilak

Reputation: 30688

NullReferenceException: Object reference not set to an instance of an object

You need to initialize properly. Seems like StatHandler.instance is static and not initialized.

You can initialize it in static constructor

class StatHandler
{

  static StatHandler()
  {
      instance = new Instance(); // Replace Instance with type of instance
  }
}

Upvotes: 0

Soner Gönül
Soner Gönül

Reputation: 98740

In C#, you can't use a value type variable without initialize it.

Looks like StatHandler.instance is static method. You can't use your int variables without any assigned. Assign some values to them.

For example

public int stamina = 1, strength = 2, agility = 3, dexterity = 4, wisdom = 5;

Upvotes: 0

VladL
VladL

Reputation: 13033

It's actually same as in Java. For non-static variables you need a class instance:

StatHandler sh = new StatHandler();
maxHealth = sh.stamina * 10;

or you can declare variables as static in the class like

public static string stamina = 10;

and then access it

maxHealth = StatHandler.stamina * 10;

Upvotes: 3

Related Questions