C# Beginner Question -- How to call a function from a separate script in multiple other scripts
This may be an obvious problem that is just not clicking to me and I've been googling for the last hour or two and has of yet, not been able to find a solution to my problem.
To start off, I'm creating an idle game and within this game, numbers can become quite large so I created a script that runs and input through a series of if-statements to determine whether it needs to be abbreviated and/or what abbreviation it needs dependent on how long the number itself is.
This script works find when testing from within the script itself. However, when I try to call it from another script in order to make different numbers auto-abbreviate, I continuously get a null exception stating the object reference is not set to an instance of the object. Being new to Unity and having not programmed in quite some time, I have absolutely no idea why this happens.
The following code is the script that converts the numbers.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class LargeNumberConverter : MonoBehaviour {
string output = "";
public string Convert(double number) {
if (number >= 1e+123) {
number /= 1e+123;
output = number.ToString ("###.000 'dd'");
return output;
} else if (number >= 1e+120) {
number /= 1e+120;
output = number.ToString ("###.000 'cc'");
return output;
// (...omitted for length...)
} else
return output;
}
}
The following script is one example of trying and failing to call the script into another script.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Click : MonoBehaviour {
public Text clickPower;
public Text followerDisplay;
public float followers = 0.00f;
public int followersPerClick = 0;
public LargeNumberConverter lnc;
void Update() {
followerDisplay.text = "Followers: " + lnc.Convert((double)followers);
clickPower.text = "Idol Power: " + lnc.Convert((double)followersPerClick);
}
public void Clicked() {
followers += followersPerClick;
}
}
I would appreciate the help of anyone willing as I'd like to get passed this as soon as possible.
Im typing this from my phone so im sorry if it gets wierd.
You need to instansiate your converter to an actual object. So try: public LargeNumberConverter lnc = new LargeNumberConverter();
Or make the converter a static class so you can call the methods that way ins$$anonymous$$d, i would probably do this but both works just as well.
do both scripts exist from the beginning? if so, assign the to the others field in the inspector.
Answer by flashframe · Jan 26, 2016 at 05:47 PM
With these kinds of helper methods, you don't really need to create an instance of your class to use them. You could make the Convert method static
public static string Convert(double number) {
}
and then you'd be able to call it from any script.
followerDisplay.text = "Followers: " + LargeNumberConverter.Convert((double)followers);
Otherwise, if you want to do it your original way, you need to actually create an instance of your class first
LargeNumberConverter lnc = new LargeNumberConverter();
Answer by Aphemra · Jan 26, 2016 at 06:00 PM
@Taxen0 and @flashframe
Thanks a ton! I totally forgot about static being a thing. Geez, it's been a long time since I opened up an IDE.
Glad to be back in it though, and thanks again!
Your answer
Follow this Question
Related Questions
When to use a namespace? 2 Answers
Am I using this Coroutine and IEnumerator correctly? 1 Answer
Could someone translate these to c#? 1 Answer
Unity Networking 1 Answer
How to make one Character follow behind another character? 0 Answers