Null refrence issues

Hello everyone, glad to be part of the unity community :slight_smile:
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.

this function is not correct:

private void setUpVitals(){

        for(int cnt = 0; cnt < _vital.Length; cnt++){

            _vital[cnt] = new Vital();  

            setUpVitalModifier();

        }

    }

here you setup the modifiers for all vitals each time you create one. it should be

private void setUpVitals(){

        for(int cnt = 0; cnt < _vital.Length; cnt++){

            _vital[cnt] = new Vital();  

        }
        setUpVitalModifier();
    }

Same goes for skills

Thanks for the quick reply. I tried what you said but I get the same errors.

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

I rearranged those, and went back to the video just to make sure “yes that is how it was meant to be”

But I am still getting the same error… This is confusing me alot!

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?

Iv gone back through the tutorial and that is defiantly how its done. As far as I can tell I really have done everything right.

Just found something odd:

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));

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?

What tutorial are you following? Is it a free series that you can link here?

It is a free series
Link to episode 1: http://www.youtube.com/watch?v=YYqzz1dy3Ak

I am up to episode 24ish: http://www.youtube.com/watch?v=Q6-9S0oVeHc&feature=bf_next&list=SPE5C2870574BF4B06

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?

Sure, I have made a rar. How do I send it or attach it to this?

If you click ‘Go Advanced’ from below the reply box you can manage attachments from there

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 :stuck_out_tongue:

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.

This is just the scripts because the whole thing was to large. Thanks for your time and help.

899232–33603–$Scripts.rar (6.78 KB)

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 :slight_smile:

I hope that helps

No worries, after reading what you said I was able to go back and fix it :slight_smile: Cheers, its coming along nicely.

Nice :slight_smile: What exactly did you do to fix it? (I could learn something here!)

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.