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.
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
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.
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();
}
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)
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>();
Thanks, $$anonymous$$avina. The last code worked perfectly. Thank you a lot for the help and attention.
Your answer
Follow this Question
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