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 Synnystter · Jun 11, 2016 at 07:19 PM · javascripterror messagenullreferenceexceptioninstance

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

Hi everyone. Well, i'm trying to make a game with RPG elements which the player have to select 3 characters, one of each type (Fighter, Especialist, Caster), but i'm having trouble trying to make the characters receive their stats when they are instanciated on the character selection screen to show them to the player.

So, here is the CharacterStats.js Script:

 #pragma strict
 import System.Collections.Generic;
 
 class CharacterBaseStats{
     var maxHealth: float;
     var maxMP: float;
     var Health: float;
     var MP: float;
     
     var baseATK: float;
     var baseMagicATK: float;
     var baseDEF: float;
     var baseMagResistence: float;
 }
 
 public enum FighterCharacterType{
     Warrior = 0,
     Paladin = 1,
     Barbarian = 2
 }
 
 class FighterCharacterTypeStats{
     var basicStats = CharacterBaseStats();
     var characterClass: FighterCharacterType;
 }
 
 var listFighterCharacterStats = List.<FighterCharacterTypeStats>();
 
 public static var instance: CharacterStats;
 
 static function GetFighterType(){
     var classID: int = PlayerPrefs.GetInt("FighterClassType");
     
     if (classID == 0){
         return FighterCharacterType.Warrior;
     } else if (classID == 1){
         return FighterCharacterType.Paladin;
     } else if (classID == 2){
         return FighterCharacterType.Barbarian;
     }
 }
 
 static function SetFighterType(characterClass: int){
     PlayerPrefs.SetInt("FighterClassType", characterClass);
 }
 
 static function GetFighterBaseStats(fighterTypeCharacter: FighterCharacterType){
     for (var info: FighterCharacterTypeStats in instance.listFighterCharacterStats){
         if(info.characterClass == fighterTypeCharacter){
             return info.basicStats;
         }
     }    
     return instance.listFighterCharacterStats[0].basicStats;
 }

I filled out the listFighterCharacterStats on the Inspector.

CharacterSelection.js:

 #pragma strict
 import System.Collections.Generic;
 
 var listFighters = List.<GameObject>();
 var listEspecialists = List.<GameObject>();
 var listCasters = List.<GameObject>();
 
 var characterFighter : GameObject;
 var characterEspecialist : GameObject;
 var characterCaster : GameObject;
 
 var i : int;
 var iFighter : int;
 var iEspecialist : int;
 var iCaster : int;
 
 function Start () {
     i = 0;
     iFighter = 0;
     iEspecialist = 0;
     iCaster = 0;
 }
 
 function Update () {
     switch(i){
         case 0 :
             SelectFighter();
             break;
         case 1 :
             SelectEspecialist();
             break;
         case 2 :
             SelectCaster();
             break;
     }
     
     if (Input.GetKeyDown(KeyCode.UpArrow)){
         i--;
         if (i < 0){
             i = listFighters.Count-1;    
         }
     } else if (Input.GetKeyDown(KeyCode.DownArrow)){
         i++;
         if (i >= listFighters.Count){
             i = 0;
         }
     }
 }
 
 function SelectFighter(){
     if (!characterFighter){
         CharacterStats.SetFighterType(iFighter);
         characterFighter = Instantiate(listFighters[iFighter], new Vector3(0, 5, transform.position.z), Quaternion.identity);
         characterFighter.AddComponent.<Fighter>();
         characterFighter.GetComponent(Rigidbody).useGravity = false;
         characterFighter.transform.LookAt(transform);
     }else{
         characterFighter.transform.Rotate(0, 90*Time.deltaTime, 0);
     }
     
     if (Input.GetKeyDown(KeyCode.LeftArrow)){
         iFighter--;
         if (iFighter < 0){
             iFighter = listFighters.Count-1;    
         }
         Destroy(characterFighter);
     } else if (Input.GetKeyDown(KeyCode.RightArrow)){
         iFighter++;
         if (iFighter >= listFighters.Count){
             iFighter = 0;
         }
         Destroy(characterFighter);
     }
 }

The SelectEspecialist() and SelectCaster() works the SelectFighter() function.

Well, both Scripts above are in the same gameObject on the scene.

This is the code attached to the prefabs that are instantiated on the CharacterSelection Script:

Fighter.js

 #pragma strict
 
 var class: FighterCharacterType;
 var stats: CharacterBaseStats;
 
 function Start () {
     class = CharacterStats.GetFighterType();
     stats = CharacterStats.GetFighterBaseStats(class);
 }

And when this code runs, Unity give me the error:

NullReferenceException: Object reference not set to an instance of an object CharacterStats.GetFighterBaseStats (FighterCharacterType fighterTypeCharacter) (at Assets/Scripts/CharacterStats.js:48) Fighter.Start () (at Assets/Scripts/Fighter.js:8)

I don't understand what's going on for the error to happens, so i need help. Sorry for any english error and any formatation error, i'm new to the forum. Thanks for the attention.

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 TBruce · Jun 11, 2016 at 07:51 PM 0
Share

Your code does not match up with your error.

You question says there is a NullReferenceException at line 79 in CharacterStats.js but there are only 55 lines?

When posting a error type question could you please point out exactly where the error is in the code?

It'll make it far easier to help you if you comment your code with something like this

 //<---- error here
avatar image Synnystter TBruce · Jun 11, 2016 at 09:36 PM 0
Share

Oh, sorry. I've done some adjustments on the code and forgot to update the error. It's on the line 48 at:

for (var info: FighterCharacterTypeStats in instance.listFighterCharacterStats){

ok, its updated now.

1 Reply

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

Answer by TBruce · Jun 11, 2016 at 10:37 PM

Here is what you have

 public enum FighterCharacterType
 {
     Warrior = 0,
     Paladin = 1,
     Barbarian = 2
 }
 
 class CharacterBaseStats
 {
     var maxHealth: float;
     var maxMP: float;
     var Health: float;
     var MP: float;
 
     var baseATK: float;
     var baseMagicATK: float;
     var baseDEF: float;
     var baseMagResistence: float;
 }
 
 class FighterCharacterTypeStats
 {
     var basicStats = CharacterBaseStats();
     var characterClass: FighterCharacterType;
 }
 
 public static var instance: CharacterStats;
 
 var listFighterCharacterStats = List.<FighterCharacterTypeStats>();

at best at this point listFighterCharacterStats is an empty list if not null.

Also you have a problem in GetFighterType() because the very first time you run your game this statement

 var classID: int = PlayerPrefs.GetInt("FighterClassType");

will set classID to -1. Instead you want this

 var classID: int = PlayerPrefs.GetInt("FighterClassType", 0);

Because of the problem in GetFighterType(), the variable class will be null when you pass it to GetFighterBaseStats() here

 stats = CharacterStats.GetFighterBaseStats(class);

Another suggestion is to place something like the following at the beginning of GetFighterBaseStats()

 if ((instance.listFighterCharacterStats == null) || (instance.listFighterCharacterStats.Count == 0))
 {
     return CharacterBaseStats();
 }
Comment
Add comment · Show 7 · 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 Synnystter · Jun 12, 2016 at 05:21 AM 0
Share

I tried your suggestions but none of them worked for me.

Also i tried to create and .Add() the itens for the listFighterCharacterStats in code ins$$anonymous$$d of set the values via inspector, but it keeps returning the same error: NullReferenceException: Object reference not set to an instance of an object.

When i tried to add

      if ((instance.listFighterCharacterStats == null) || (instance.listFighterCharacterStats.Count == 0))
      {
          return CharacterBaseStats();
      }

to the code it also gave me the very same message error, but on the line

  if ((instance.listFighterCharacterStats == null) || (instance.listFighterCharacterStats.Count == 0))

ins$$anonymous$$d of line

 for (var info: FighterCharacterTypeStats in instance.listFighterCharacterStats)
avatar image TBruce Synnystter · Jun 12, 2016 at 05:33 AM 0
Share

Do this at the beginning of GetFighterBaseStats()

 if (instance == null)
 {
     Debug.Log("instance == null");
     return CharacterBaseStats();
 }
 else if (instance.listFighterCharacterStats == null)
 {
     Debug.Log("instance.listFighterCharacterStats == null");
     return CharacterBaseStats();
 }
 else if (instance.listFighterCharacterStats.Count == 0)
 {
     Debug.Log("instance.listFighterCharacterStats.Count == 0");
     return CharacterBaseStats();
 }
 else
 {
     Debug.Log("Default");
     return CharacterBaseStats();
 }

which statement does it fall into? The first one?

Oh and move

 public static var instance: CharacterStats;

above

 var listFighterCharacterStats = List.<FighterCharacterTypeStats>();
avatar image Synnystter TBruce · Jun 12, 2016 at 08:46 PM 0
Share

Yeah. It fall on "instance == null".

Show more comments
avatar image Synnystter · Jun 13, 2016 at 05:45 PM 0
Share

Thanks, $$anonymous$$avina. The last code worked perfectly. Thank you a lot for the help and attention.

Show more comments

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

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

Related Questions

javascript NullReferenceException: Object reference not set to an instance of an object 0 Answers

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

A certain string is causing trouble! (Javascript) 0 Answers

Error BCE0019: 'refusethrow' is not a member of 'UnityEngine.GameObject'. 0 Answers

Bracket error 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