Hello everyone, glad to be part of the unity community
I have been playing around with unity in a tutorial and am really pleased with it so far, but I almost always get null reference errors. For the most part I can fix them but this one has been a real pain!
This is my code:
using UnityEngine;
using System.Collections;
using System; //added for enum
public class baseCharacter : MonoBehaviour {
private string _name;
private int _level;
private uint _freeExp;
private Attribute[] _primaryAttribute;
private Vital[] _vital;
private Skill[] _skill;
public void Awake(){
_name = string.Empty;
_level = 0;
_freeExp = 0;
_primaryAttribute = new Attribute[Enum.GetValues(typeof(attributeName)).Length];
_vital = new Vital[Enum.GetValues(typeof(VitalName)).Length];
_skill = new Skill[Enum.GetValues(typeof(SkillName)).Length];
setUpSkills();
setUpPrimaryAttribute();
setUpVitals();
}
public string Name {
get{return _name; }
set{_name = value; }
}
public int Level {
get{return _level; }
set{_level = value; }
}
public uint FreeExp {
get{return _freeExp; }
set{_freeExp = value; }
}
public void addExp(uint exp){
_freeExp += exp;
calculateLevel();
}
public void calculateLevel(){
}
private void setUpPrimaryAttribute(){
for(int cnt = 0; cnt < _primaryAttribute.Length; cnt++){
_primaryAttribute[cnt] = new Attribute();
}
}
private void setUpVitals(){
for(int cnt = 0; cnt < _vital.Length; cnt++){
_vital[cnt] = new Vital();
setUpVitalModifier();
}
}
private void setUpSkills(){
for(int cnt = 0; cnt < _skill.Length; cnt++){
_skill[cnt] = new Skill();
setUpSkillModifier();
}
}
public Attribute getPrimaryAttribute(int index){
return _primaryAttribute[index];
}
public Vital getVital(int index){
return _vital[index];
}
public Skill getSkill(int index){
return _skill[index];
}
private void setUpVitalModifier(){
//health
getVital((int)VitalName.Health).addModifier(new modifingAttribute(getPrimaryAttribute((int)attributeName.Consitution), .5f));
//energy
getVital((int)VitalName.Energy).addModifier(new modifingAttribute(getPrimaryAttribute((int)attributeName.Consitution), 1));
//mana
getVital((int)VitalName.Mana).addModifier(new modifingAttribute(getPrimaryAttribute((int)attributeName.Willpower), .5f));
}
private void setUpSkillModifier(){
//melee offence
getSkill((int)SkillName.Melee_Offence).addModifier(new modifingAttribute(getPrimaryAttribute((int)attributeName.Might), 33f));
getSkill((int)SkillName.Melee_Offence).addModifier(new modifingAttribute(getPrimaryAttribute((int)attributeName.Dexterity), 33f));
}
public void statUpDate(){
for(int cnt = 0; cnt < _vital.Length; cnt ++){
_vital[cnt].update();
}
for(int cnt = 0; cnt < _skill.Length; cnt ++){
_skill[cnt].update();
}
}
}
And these are my errors:
NullReferenceException: Object reference not set to an instance of an object
baseCharacter.setUpVitalModifier () (at Assets/Scripts/CharacterClasses/baseCharacter.cs:86)
baseCharacter.setUpVitals () (at Assets/Scripts/CharacterClasses/baseCharacter.cs:60)
baseCharacter.Awake () (at Assets/Scripts/CharacterClasses/baseCharacter.cs:26)
characterGen.Start () (at Assets/Scripts/CharacterClasses/characterGen.cs:15)
NullReferenceException: Object reference not set to an instance of an object
baseCharacter.statUpDate () (at Assets/Scripts/CharacterClasses/baseCharacter.cs:99)
characterGen.Update () (at Assets/Scripts/CharacterClasses/characterGen.cs:25)
Any help getting rid of these would be great, thanks.
just go back through the videoâs, re-check all your code. your error is bound to show up somewhere. I did these same vids and was confused sometimes as well.
It will help to watch the videoâs where these classes are created again (or just go through them).
EDIT:
forget what i said⌠the problem is that in your setupSkillModifier function you presume that the attributes are already setup.
In your awake function, try to first call setupPrimaryAttributes and then the skills and vitals
Without seeing the supporting scripts to see all the class and function declarations, itâs hard to tell exactly whatâs going on. As a wild stab in the dark on line 86 you pass an Int value as the last parameter, but in all other cases you pass a float. Is that right according to whatever tutorial your following?
Generally if I click on somthing in the code it will highlight all the other instances of that name/call and so on. But when I like on âgetVitalâ all of them high lite but the ones for energy and mana. Is that important?
I donât see anything obviously wrong, but without following the entire series itâs hard to be sure. Is the error you get when the script is compiling or when you press the Play button?
Could you upload a .zip of the project or the just the Scripts folder?
Someone posts this problem every other week. From the same tutorial. Leads me to believe that the tutorial creator has just done it all wrong or people canât read
All of his other videos are fine, yes some people have an issue but it does work. I think its more that he goes through to quickly and the video is only in 720p making it hard to see some on the finer details the the difference between ( { or , . easy to see now but hard in lower resolutions.
After looking a the scripts, it appears the problems your having relate to your instance of the playerCharacter class (_toon) and everytime you try to call a function from it after itâs creation. If you look at the script references at the bottom of the error calls youâll see the lines in characterGen that the problem is coming from.
You also have this warning at the top which I think is relevant:
You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
Which makes me think this part of your characterGen script is wrong, and thus your calling functions for an object that hasnât correctly been created (hence the errors)
private playerCharacter _toon;
...
_toon = new playerCharacter();
Unfortunately I donât know enough about using C# and classes in Unity to be able to help any further (I do most of my work in Javascript), but hopefully someone can point you in the right direction now
It was a strange one, in the tutorial he used a basic for loop
for (int blah = 0: blah blah blah ; blah++)
But he didnât use the { } to open or close it, I did just because I have learned to always do it. Once I removed them it worked fine so strange, I must have been accidentally closing off a different loop or function. I canât remember the exact line it was though⌠I think it was in baseState.