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 matteonite · Oct 21, 2012 at 12:18 AM · c#errorimportburgzergarcade

Burgzergarcade Why am i getting an error after re-importing?

I have been following Burgzergarcade's tutorials and i had made a back up of my project to test something. And when I went to re-import it, everything seemed to work but when I play it I get an error.

This is the error:

NullReferenceException: Object reference not set to an instance of an object GameSettings.LoadCharacterData () (at Assets/Scripts/GameSettings.cs:48) GameMaster.LoadCharacter () (at Assets/Scripts/GameMaster.cs:61) GameMaster.Start () (at Assets/Scripts/GameMaster.cs:47)

Here is my GameSettings.cs:

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class GameSettings : MonoBehaviour {
     public const string PLAYER_SPAWN_POINT = "Player Spawn Point";        //this is the name of the gameObject that the player will spawn at at the start of the level
     
     void Awake(){
         DontDestroyOnLoad(this);                                        //allows script to surive from scene to scene
     }
     
     public void SaveCharacterData(){
         GameObject pc = GameObject.Find("pc");
         
         PlayerCharacter pcClass = pc.GetComponent<PlayerCharacter>();
         
         //PlayerPrefs.DeleteAll();        //use when adding or editing Vitals, Attributes, or Skills *Note: uncomment (Set - Mods when this is done)
         
         PlayerPrefs.SetString("Player Name", pcClass.Name);
         
         for(int cnt = 0 ; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++){
             PlayerPrefs.SetInt(((AttributeName)cnt).ToString() + " - Base Value", pcClass.GetPrimaryAttribute(cnt).BaseValue);
             PlayerPrefs.SetInt(((AttributeName)cnt).ToString() + " - Exp To Level", pcClass.GetPrimaryAttribute(cnt).ExpToLevel);
         }
         for(int cnt = 0 ; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++){
             PlayerPrefs.SetInt(((VitalName)cnt).ToString() + " - Base Value", pcClass.GetVital(cnt).BaseValue);
             PlayerPrefs.SetInt(((VitalName)cnt).ToString() + " - Exp To Level", pcClass.GetVital(cnt).ExpToLevel);
             PlayerPrefs.SetInt(((VitalName)cnt).ToString() + " - Cur Value", pcClass.GetVital(cnt).CurValue);
         
 //            PlayerPrefs.SetString(((VitalName)cnt).ToString() + " - Mods", pcClass.GetVital(cnt).GetModifyingAttributeString());
             
         }
         for(int cnt = 0 ; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++){
             PlayerPrefs.SetInt(((SkillName)cnt).ToString() + " - Base Value", pcClass.GetSkill(cnt).BaseValue);
             PlayerPrefs.SetInt(((SkillName)cnt).ToString() + " - Exp To Level", pcClass.GetSkill(cnt).ExpToLevel);
         
 //            PlayerPrefs.SetString(((SkillName)cnt).ToString() + " - Mods", pcClass.GetSkill(cnt).GetModifyingAttributeString());
                         
         }
     
     }
         
     public void LoadCharacterData(){
         GameObject pc = GameObject.Find("pc");
         
         PlayerCharacter pcClass = pc.GetComponent<PlayerCharacter>();
         
         pcClass.Name = PlayerPrefs.GetString("Player Name", "Name Me");
         
         for(int cnt = 0 ; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++){
             pcClass.GetPrimaryAttribute(cnt).BaseValue = PlayerPrefs.GetInt(((AttributeName)cnt).ToString() + " - Base Value", 0);
             pcClass.GetPrimaryAttribute(cnt).ExpToLevel    = PlayerPrefs.GetInt(((AttributeName)cnt).ToString() + " - Exp To Level", Attribute.STARTING_EXP_COST);
         }    
         
         for(int cnt = 0 ; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++){
             pcClass.GetVital(cnt).BaseValue = PlayerPrefs.GetInt(((VitalName)cnt).ToString() + " - Base Value", 0);
             pcClass.GetVital(cnt).ExpToLevel = PlayerPrefs.GetInt(((VitalName)cnt).ToString() + " - Exp To Level", 0);
             
             //make sure you call this so that the AdjustedBaseValue will be updated before you try to call to get the curValue
             pcClass.GetVital(cnt).Update();
             
             //get the stored value for the curValue foreach vital
             pcClass.GetVital(cnt).CurValue = PlayerPrefs.GetInt(((VitalName)cnt).ToString() + " - Cur Value", 1);
         }
         
         for(int cnt = 0 ; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++){
             pcClass.GetSkill(cnt).BaseValue = PlayerPrefs.GetInt(((SkillName)cnt).ToString() + " - Base Value", 0);
             pcClass.GetSkill(cnt).ExpToLevel = PlayerPrefs.GetInt(((SkillName)cnt).ToString() + " - Exp To Level", 0);
                 
         }
         //output the curValue for each of the vitals
         for(int cnt = 0; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++){
 //            Debug.Log(((SkillName)cnt).ToString() + ": " + pcClass.GetSkill(cnt).BaseValue + " - " + pcClass.GetSkill(cnt).ExpToLevel);
         }    
     }    
 }



And here is my GameMaster.cs :

 using UnityEngine;
 using System.Collections;
 
 public class GameMaster : MonoBehaviour {
     public GameObject playerCharacter;
     public GameObject gameSettings;
     public Camera mainCamera;
         
     public float zOffset;
     public float yOffset;    
     public float xRotOffset;
     
     private GameObject _pc;
     private PlayerCharacter _pcScript;
     
     public Vector3 _playerSpawnPointPos;            //this is the place where i want my player to spawn.
     
     // Use this for initialization
     void Start () {
 //        _playerSpawnPointPos = new Vector3(466, 145, 720);    // the default positition for our player spawn point (used to be 1008, 66, 212)
         
         GameObject go = GameObject.Find(GameSettings.PLAYER_SPAWN_POINT);
         
         if(go == null){
             Debug.LogWarning("Can not find Player Spawn Point");    
             
             go = new GameObject(GameSettings.PLAYER_SPAWN_POINT);
             Debug.Log("Created Player Spawn Point");    
             
             go.transform.position = _playerSpawnPointPos;
             Debug.Log("Moved Player Spawn Point");    
 
         }    
         
         _pc = Instantiate(playerCharacter, go.transform.position, Quaternion.identity)as GameObject;
         _pc.name = "pc";
         
         _pcScript =_pc.GetComponent<PlayerCharacter>();
         
         zOffset = -2.5f;
         yOffset = 2.5f;
         xRotOffset = 22.5f;
         
         mainCamera.transform.position = new Vector3(_pc.transform.position.x, _pc.transform.position.y + yOffset, _pc.transform.position.z + zOffset);
         mainCamera.transform.Rotate(xRotOffset, 0, 0);
     
         LoadCharacter();
     }
     
     public void LoadCharacter(){
         GameObject gs = GameObject.Find("__GameSettings");
         
         if(gs = null){
             GameObject gs1 = Instantiate(gameSettings, Vector3.zero, Quaternion.identity) as GameObject;
             gs1.name = "__GameSettings";
             
         }
         GameSettings gsScript = GameObject.Find("__GameSettings").GetComponent<GameSettings>();
             
         //load the character data
         gsScript.LoadCharacterData();    
     }
 }


If you see any errors in my script or if you need more info let me know.

Thank You.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by harko12 · Nov 08, 2012 at 04:20 AM

What you need to do is look at the lines that the error is telling you, and see which object is null that shouldn't be. Anytime you do a method on an object, like pc.GetComponent<>(), if pc is null, you will get that error. Anyway, if you debug into your game you will probably see which object is null, and then you can look at why it's not getting set correctly.

So, just looking at your code what it's telling you is that in the GameSettings.LoadCharacterData method, on line 48, there was a null object reference error, like I described above. If you look at that method in GameSettings you see:

     public void LoadCharacterData(){
        GameObject pc = GameObject.Find("pc");
 
        PlayerCharacter pcClass = pc.GetComponent<PlayerCharacter>();
 
        pcClass.Name = PlayerPrefs.GetString("Player Name", "Name Me"); <-- line 48
 
        for(int cnt = 0 ; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++){
 

There are two object methods getting called in that line. pcClass.Name and PlayerPrefs.GameString(. Either pcClass or PlayerPrefs is null, which means it's not getting set up correctly anymore. You need to debug in and find out why.

Once you do that, remember this process, because you'll have to do it a million times. Welcome to Programming :)

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 rooster2 · Oct 22, 2016 at 02:08 PM 0
Share

this doesnt say how to fix this error tho so you aint useful

avatar image harko12 rooster2 · Oct 23, 2016 at 03:05 AM 0
Share

Well it's been four years, so hopefully he's been able to follow my advice on how to fix the problem and take care of it.

I did tell him what the error meant, and how to find out why it's happening, so aside from flying to his house, and debugging it in person I'm not sure what else you expected from me.

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

11 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

Related Questions

SetActive() Object reference not set to an instance of an object. 1 Answer

Array index out of range error 1 Answer

Problem with messenger extended 0 Answers

Multiple Cars not working 1 Answer

Problem creating GUI.Window, Help please! 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