Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Olgo · Jul 18, 2013 at 06:36 PM · objectinstanceobject reference

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!

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Linus · Jul 18, 2013 at 07:09 PM 0
Share

Have you tried without the UnityEngine prefix Random.Range(0, races.Length)

avatar image Olgo Linus · Jul 18, 2013 at 07:18 PM 0
Share

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

avatar image Sisso · Jul 18, 2013 at 10:09 PM 0
Share

What is Race? It extends or belongs something that extends $$anonymous$$onobehaviour?

avatar image Olgo · Jul 19, 2013 at 12:28 PM 0
Share

Race is an enumerator. I'm storing possible races of a character, i.e., Human, Elf, Dwarf etc

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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)];
 
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Slobdell · Jul 19, 2013 at 12:01 AM 0
Share

Sorry

 Race[] races = Enum.GetValues(typeof(Race));
 Race characterRace = races[UnityEngine.Random.Range(0, races.Length)];
avatar image Slobdell · Jul 19, 2013 at 12:08 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges