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 webik150 · Sep 28, 2014 at 09:23 AM · nullreferenceexceptionplayerprefssaveloadpokemon

Null Reference Exception even after check

Hey. I've run into a little problem today. I have a script to save and load my pokémon party to PlayerPrefs, but for some reason it isn't working. Whenever I try to save, I get:

 NullReferenceException: Object reference not set to an instance of an object
 Party.savePokemon () (at Assets/Scripts/Party.cs:21)

This is my code:

 public Pokemon[] party = new Pokemon[6];
 public InitPokemon pokeDB = new InitPokemon();
 
 public void savePokemon(int savenumber){
         for (int i = 0; i < party.Length; i++) {
             if (party[i].ID != null) {
             PlayerPrefs.SetInt("Player."+savenumber+".Pokemon.Party."+i+".ID", party[i].ID);   //LINE 21!
             PlayerPrefs.SetString("Player."+savenumber+".Pokemon.Party."+i+".Nickname", party[i].nickname);
             PlayerPrefs.SetInt("Player."+savenumber+".Pokemon.Party."+i+".Attack", party[i].Attack);
             PlayerPrefs.SetInt("Player."+savenumber+".Pokemon.Party."+i+".SpAttack", party[i].SpAttack);
             PlayerPrefs.SetInt("Player."+savenumber+".Pokemon.Party."+i+".Defense", party[i].Defense);
             PlayerPrefs.SetInt("Player."+savenumber+".Pokemon.Party."+i+".SpDefense", party[i].SpDefense);
             PlayerPrefs.SetInt("Player."+savenumber+".Pokemon.Party."+i+".Speed", party[i].Speed);
             PlayerPrefs.SetInt("Player."+savenumber+".Pokemon.Party."+i+".HP", party[i].HP);
             PlayerPrefs.SetInt("Player."+savenumber+".Pokemon.Party."+i+".CurrentHP", party[i].currentHP);
             PlayerPrefs.SetInt("Player."+savenumber+".Pokemon.Party."+i+".CurrentEXP", party[i].currentExp);
             }
         }
     }
     public void loadPokemon(int savenumber){
 
 
         for (int i = 0; i < party.Length; i++) {
             if(PlayerPrefs.HasKey("Player."+savenumber+".Pokemon.Party."+i+".ID")){
                 party[i] = pokeDB.pokemonArray[PlayerPrefs.GetInt("Player."+savenumber+".Pokemon.Party."+i+".ID")-1];
                 party[i].ID = PlayerPrefs.GetInt("Player."+savenumber+".Pokemon.Party."+i+".ID");
                 party[i].nickname = PlayerPrefs.GetString("Player."+savenumber+".Pokemon.Party."+i+".Nickname");
                 party[i].Attack = PlayerPrefs.GetInt("Player."+savenumber+".Pokemon.Party."+i+".Attack");
                 party[i].SpAttack = PlayerPrefs.GetInt("Player."+savenumber+".Pokemon.Party."+i+".SpAttack");
                 party[i].Defense = PlayerPrefs.GetInt("Player."+savenumber+".Pokemon.Party."+i+".Defense");
                 party[i].SpDefense = PlayerPrefs.GetInt("Player."+savenumber+".Pokemon.Party."+i+".SpDefense");
                 party[i].Speed = PlayerPrefs.GetInt("Player."+savenumber+".Pokemon.Party."+i+".Speed");
                 party[i].HP = PlayerPrefs.GetInt("Player."+savenumber+".Pokemon.Party."+i+".HP");
                 party[i].currentHP = PlayerPrefs.GetInt("Player."+savenumber+".Pokemon.Party."+i+".CurrentHP");
                 party[i].currentExp = PlayerPrefs.GetInt("Player."+savenumber+".Pokemon.Party."+i+".CurrentEXP");
                 }
         }
     }


Any idea what I have done wrong/how to fix this?

EDIT: The Null is probably pointing towards party[i].ID, because when I change it to set 1, the error start to point on line 22. However I have the party set up in the editor, + there's the check so I don't get why it isn't working.

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 gjf · Sep 28, 2014 at 09:28 AM 0
Share

seems like the fragment of code posted has a blank line 21 (as reported by the error message) - which line does it refer to?

avatar image webik150 · Sep 28, 2014 at 09:40 AM 0
Share

The code is shortened of Update() and Start(). There is comment on the real 21. line. Here it would be line 7

avatar image Baste · Sep 28, 2014 at 09:45 AM 2
Share

If the error is in party[i].ID, that means that party[i] is null for that value of i. This either means that it hasn't been assigned, or that it has been destroyed.

Try a Debug.Log(party[i]) just before where the error occurs, that'll clue you in to what's happening.

avatar image gjf · Sep 28, 2014 at 10:06 AM 0
Share

if you haven't initialized each entry of party[] then you will receive the null reference exception. in c# it's not enough just to new the array/list/etc.

 public Pokemon[] party;
 
 private void InitParty(int partySize)
 {
     party = new Pokemon[partySize];
 
     for (var i = 0; i < partySize; i++)
     {
         party[i] = new Pokemon();
     }
 }

then add a call to InitParty() somewhere in your Awake()/Start()/etc.

 InitParty(6);

you might consider initializing the entries with default values too...

EDIT: just saw the OP's edit so not sure it's an initialization issue...

1 Reply

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

Answer by webik150 · Sep 28, 2014 at 10:09 AM

Um... Okay I'm just stupid. The check should have been if(party[i] != null)

Thanks Baste

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

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

26 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Saving and loading game with PlayerPrefs 1 Answer

How can I continue the game without losing values after Game Over? 1 Answer

Playerprefs doesn't save anything. 2 Answers

save string from array to player prefs 1 Answer

How can playerprefs save my tower model and its position? 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