- Home /
Why does this cause an "object reference not set to an instance of an object?"
Hey guys, my code compiles and runs, but for some reason I get "object reference not set to an instance of an object" in reference to the second line. if I replace it with: string randomRace = races[0];
it remains. if i replace it with string randomRace = "Human";
the issue goes away. so I know it has something to do with "races[]" in the second line, i just don't understand what.
string[] races = Enum.GetValues(typeof(Race)) as string[];
string randomRace = races[UnityEngine.Random.Range(0, races.Length)];
characterRace = (Race) Enum.Parse( typeof( Race ), randomRace );
Thanks!
Have you tried without the UnityEngine prefix Random.Range(0, races.Length)
Yup. As stated in my original question. I've changed it to "randomRace = races[0]" and the problem remains. The UnityEngine is required because i'm using System namespace and that creates opportunity for more than 1 Random.
If you could, change your answer to a comment so people don't pass this up as answered. Thanks
What is Race? It extends or belongs something that extends $$anonymous$$onobehaviour?
Race is an enumerator. I'm storing possible races of a character, i.e., Human, Elf, Dwarf etc
Answer by Olgo · Jul 23, 2013 at 05:17 PM
Turns out my first line of code reads:
string[] races = Enum.GetValues(typeof(Race)) as string[];
but it should read:
string[] races = Enum.GetNames(typeof(Race)) as string[];
GetNames returns the string value associated with an Enum value, GetValues returns the numerical int value associated with the Enum value.
Answer by Slobdell · Jul 18, 2013 at 11:51 PM
It's because your first like isn't returning anything, check the length of races and you will see it doesn't exist. Remove the "as string[]". Enum.GetValues returns an array of the type you specified, so what you're trying to do is cast a Race object to a string....which won't work. You've actually made this more complicated than it should be, changing those 3 lines to just this:
string[] races = Enum.GetValues(typeof(Race));
Race characterRace = races[UnityEngine.Random.Range(0, races.Length)];
Sorry
Race[] races = Enum.GetValues(typeof(Race));
Race characterRace = races[UnityEngine.Random.Range(0, races.Length)];
Actually this won't work either. You're not storing actual Race objects in the enum so you can't get a Race object out. Will have to be something like
string[] Races = Enum.GetValues(typeof(string));
string characterRace = races[UnityEngine.Random.Range(0, races.Length - 1)];
if(characterRace.equals("Race1")){
"Get Race1 from wherever you're getting it from"
}else if(etc.)....
Your answer
Follow this Question
Related Questions
How to instantiate object onto other object from script 1 Answer
object not set to instance? 1 Answer
Script decides not to run anymore 1 Answer
NullReferenceException: Object reference not set to an instance of an object 1 Answer
NullReferenceException: Object reference not set to an instance of an object ..... 1 Answer