Call a method from another class

27.4k views Asked by At

I'm wanting to know how I can call a method from another class without having to make a new instance of that class. I've looked up this and 90% of the examples I see require me to make a new copy of my referenced class.

Something like this:

Fooclass test = new Fooclass();
test.CallMethod();

However, I'm wondering if there is a way I can call the method without making a new class instance. Right now I've tried the following in unity.

public ImageLoader image; 
void Start () 
{
    image = gameObject.GetComponent<ImageLoader>() as ImageLoader;
}

void OnClick()
{
    image.MoveForward();
}

however, when I run this I get the following error:

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

i know this would be settled with making a new instance of my image loader class but I can't do that as it is holding a lot of data I don't want duplicated multiple times.

5

There are 5 answers

3
Christos On BEST ANSWER

Yes you can. The first way is to make your class to be static.

public static class Fooclass
{
    // I don't know the return type of your CallMethod, so I used the void one.
    public static void CallMethod()
    {

    }
}

This way, whenever to your code you can call the CallMethod() like the following:

Fooclass.CallMethod()

Another apporach it would be to define a static method in your current class, without the class needed to be static, like the following:

public class Fooclass
{
    // I don't know the return type of your CallMethod, so I used the void one.
    public static void CallMethod()
    {

    }
}

Now since all the instances of the Fooclass would share the same method called CallMethod, you can call it like below:

Fooclass.CallMethod()

without again needed to instantiate an object of type Fooclass, despite the fact that now Fooclass isn't a static class now !

For further documentation please take a look to the link Static classes and Static Members.

0
Sadique On

Make a static class / static method. Making the method static is good enough if you do not want your class to be static.

class my_Class
{
    public static void Print()
    {
        Console.WriteLine("Hello World");
    }
}

my_Class.Print();
1
Ehsan On

If the method of your other class is not using any instance level variables then you can make it static and use it like

Fooclass.CallMethod();
0
Shishir Kumar On

You need to declare a static member in your static/non-static class. The static member is callable on a class even when no instance of the class has been created.

For eg:

public class Automobile
{
    public static int NumberOfWheels = 4;
    public static int SizeOfGasTank
    {
        get
        {
            return 15;
        }
    }
    public static void Drive() { }
    public static event EventType RunOutOfGas;

    // Other non-static fields and properties...
}

Static members are initialized before the static member is accessed for the first time and before the static constructor, if there is one, is called. To access a static class member, use the name of the class instead of a variable name to specify the location of the member, as shown in the following example:

Automobile.Drive();
int i = Automobile.NumberOfWheels;

The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created. Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.

Source: http://msdn.microsoft.com/en-us/library/79b3xss3.aspx

Hope this helps you.

Shishir

0
Alexander Müller On

The marked answers fit to your question, however please also note that your original Problem seems to be different.

I wonder if the original Problem that results in your NullReferenceException: Object reference not set to an instance of an object error is that your method named OnClick() is either called before your Start() method or that the call image = gameObject.GetComponent<ImageLoader>() as ImageLoader; never returns a valid instance of your desired class, therefore image is always null.