- Home /
NullReferenceException
Let's say that I have a public static List in one script.
public static List<String> ClientNames;
Calling it from other script in specifically OnGUI()
function gives me NullReferenceException
.
ScriptName.ClientNames.Add("Rafael");
It returns to me: NullReferenceException: Object reference not set to an instance of an object
I really have to do: ScriptName instanceName = GetComponent();
?
Are you calling a javascript from a c# script or vice-versa... ? If you are there are access issues between scripts..
I'm already aware of that, I just use C#, thanks for the note. =)
Answer by rutter · Jul 31, 2014 at 05:45 AM
As it turns out, any "reference" value can end up pointing at nothing, which your system calls "null". If you try to access a reference which is currently null
, your computer will warn you that you're trying to access something which doesn't exist. You need to make sure it exists before accessing it.
It's important to distinguish two actions:
Declaring a variable gives it a name
Assigning a variable gives it a value
You've declared, but you haven't assigned.
ClientNames = new List<String>();
You can do this all at once, too:
public static List<String> ClientNames = new List<String>();
You are right, and I'm ashamed because I forgot a simple thing like that. Actually I did a test code before and I assigned the variable before, but then I erased everything and now it turns out that maybe I need glasses haha, just kidding. Thank you rutter!
Your answer
![](https://koobas.hobune.stream/wayback/20220613155440im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to check for null 2 Answers
NullRefenceException error 1 Answer
NullReferenceException for reasons I don't understand. 1 Answer