Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by diggerplus · Jan 11, 2016 at 03:49 PM · c#scripting problemnullreferenceexception

getting a NullReferenceException error and can't see why.

Been following along with a tutorial on YouTube by a guy named HardlyBriefDan. He's doing a tutorial on how to create a basic RPG game. The guy seems to know what he's doing and I'm right there with him doing all he does, but I'm getting an error. I tried posting on the video page, messaging the guy directly, but not getting any help. Was suggested I ask here, so here it is.

The error:

NullReferenceException: Object reference not set to an instance of an object

The Script:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement;
 
 public class CreateNewCharacter : MonoBehaviour {
 
     private BasePlayer newPlayer;
     private bool isMageClass;
     private bool isWarriorClass;
     private bool isRogueClass;
     private bool isClercClass;
     private bool isRangerClass;
     private bool isDruidClass;
     private string playerName;
 
     // Use this for initialization
     void Start () {
         newPlayer = new BasePlayer();
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void OnGUI()
     {
         // creates a name and limits it to 15 letters
         playerName = GUILayout.TextArea(playerName, 15);
         // choose your class
         isMageClass = GUILayout.Toggle(isMageClass, "Mage Class");
         isWarriorClass = GUILayout.Toggle(isWarriorClass, "Warrior Class");
         isRogueClass = GUILayout.Toggle(isRogueClass, "Rogue Class");
         isClercClass = GUILayout.Toggle(isClercClass, "Cleric Class");
         isRangerClass = GUILayout.Toggle(isRangerClass, "Ranger Class");
         isDruidClass = GUILayout.Toggle(isDruidClass, "Druid Class");
         if (GUILayout.Button("Create"))
         {
             if (isMageClass) { newPlayer.PlayerClass = new BaseMageClass(); }
             else if (isWarriorClass) { newPlayer.PlayerClass = new BaseWarriorClass(); }
             else if (isRogueClass) { newPlayer.PlayerClass = new BaseRogueClass(); }
             else if (isClercClass) { newPlayer.PlayerClass = new BaseClericClass(); }
             else if (isRangerClass) { newPlayer.PlayerClass = new BaseRangerClass(); }
             else if (isDruidClass) { newPlayer.PlayerClass = new BaseDruidClass(); }
             // start player at level 1 and get the values set for them
             newPlayer.PlayerLevel = 1;
             newPlayer.Stregnth = newPlayer.PlayerClass.Stregnth;
             newPlayer.Intelegence = newPlayer.PlayerClass.Intelegence;
             newPlayer.Constitution = newPlayer.PlayerClass.Constitution;
             newPlayer.Dexterity = newPlayer.PlayerClass.Dexterity;
             newPlayer.Charisma = newPlayer.PlayerClass.Charisma;
             newPlayer.Wisdom = newPlayer.PlayerClass.Wisdom;
             // get player name and additional information
             newPlayer.PlayerName = playerName;
             StoreNewPlayerInfo();
             SaveInformation.SaveAllInformation();
         }
         if (GUILayout.Button("Load")) { SceneManager.LoadScene("test"); }
     }
     // store all information on empty GameInformation Object
     private void StoreNewPlayerInfo()
     {
         GameInformation.PlayerName = newPlayer.PlayerName;
         GameInformation.PlayerLevel = newPlayer.PlayerLevel;
         GameInformation.Stregnth = newPlayer.Stregnth;
         GameInformation.Intelegence = newPlayer.Intelegence;
         GameInformation.Constitution = newPlayer.Constitution;
         GameInformation.Dexterity = newPlayer.Dexterity;
         GameInformation.Charisma = newPlayer.Charisma;
         GameInformation.Wisdom = newPlayer.Wisdom;
     }
 }

I also think the guy in the tutorial is using version 4.5 and I am using version 5.3

thanks in advance.

Comment
Add comment · Show 2
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 ZacharySBlubaugh · Sep 21, 2016 at 06:22 PM 0
Share

I have a problem with toggles throwing null reference error exception also, if it calls a function in a script it NREE's. They always work like I expect. I think it's something with Unity.

avatar image NoseKills ZacharySBlubaugh · Sep 22, 2016 at 07:02 AM 0
Share

You should post the exact error message with the line number. There's about a 100 things in that script that could throw an NRE and juat by reading the error message narrows it down to a few.

You can't fix an NRE without knowing exactly which variable is the null one. Finding out that is the first step. Finding out why is the second

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by SirNiklas · Sep 25, 2016 at 05:42 PM

It seems that the guy you are watching tutorials from does not know what he is doing. On top the video is old, which shows itself by the use of the OnGUI method.

NoseKills already wrote it, to exactly tell you where the mistake lies I have to see the error message. My guess is that the Start method is not called before the first enter into OnGUI which leads to newPlayer being accessed although it is not referencing any object.

The boolean fields in the CreateNewCharacter type are not good practice. Rather you should use an enum which contains every class choosable from.

 [Serializable]
 public enum PlayerClass
 {
    Warrior,
    Rogue,
    Clerc,
    Ranger,
    Druid
 }

Now you can use a switch to check the chosen class and create a new player from this information.

 private void ChooseClass(PlayerClass class)
 {
    switch (class)
    {
       case PlayerClass.Warrior:
          newPlayer = new BaseWarriorClass();
          break;
 
       // And so on
    }
 
    // Its way simpler to just store a BasePlayer reference in the GameInformation static type than to assign every important value one by one
    GameInformation.player = newPlayer;
 }

I declared a new method for the process of creating and setting the character because UI and game logic should normally be strictly separated.

Comment
Add comment · Show 1 · 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 Mattthhdp · Jun 14, 2017 at 06:34 PM 0
Share

When you do this GameInformation.player = newPlayer; how do you access information ? like the player hp example?

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

7 People are following this question.

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

Related Questions

NullReferenceException in FiniteStateMachine with ThirdPersonCharacter 0 Answers

Object reference not set to an instance of an object - Jumping Scripts (C#) 1 Answer

Null Reference in UnityStandardAssets.Utility.WaypointProgressTracker.Update 0 Answers

Why this if logic isnot short circuiting? 0 Answers

Null Reference Exception only in second method 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