5

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.

2
  • you could make the method static and call it using ClassName.Method(). Commented Mar 18, 2014 at 10:26
  • Regarding your code, you do not need as ImageLoader as GetComponent<ImageLoader> already has casted it for you. Regarding your error, the gameObject you're referring to likely does not have an ImageLoader component assigned to it; double-check that it's there. Commented Mar 18, 2014 at 10:41

5 Answers 5

8

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.

Sign up to request clarification or add additional context in comments.

3 Comments

The class doesn't need to be static, no matter the place of usage. It's just, if the class contains only static members, making the class static prevents people from creating a class instance that is not intended to provide any instance specific functionality.
@grek40 This is why I have mentiond the second approach, where you can define a static method in a non static class. Of course you don't have to mark a class as static if your only intention is one of the class methods to be called without creating an object.
It might be a wording issue but to me it reads as if it would be important to have the static method in the current class in order to make it work with a static method in a non-static class.
2

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(); 

Comments

1

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(); 

1 Comment

Another workaround would be to make the data static. He has problems with ImageLoader being too big, why not just make THAT static?
1

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.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.