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.
ClassName.Method().as ImageLoaderasGetComponent<ImageLoader>already has casted it for you. Regarding your error, thegameObjectyou're referring to likely does not have anImageLoadercomponent assigned to it; double-check that it's there.