- Home /
The question is answered, right answer was accepted
NullReferenceException: Object reference not set to an instance of an object
This is Almost the same error as before but This Stops the game totaly.
It points at lines: 44 and 18.
It doesn't display the skills correctly like Melee_ Offence and mele_defence and so on. I'm watching this tutorial:
using UnityEngine; using System.Collections; using System;
public class CharacterGenerator : MonoBehaviour {
private PlayerCharacter _toon;
// Use this for initialization
void Start () {
_toon = new PlayerCharacter();
_toon.Awake();
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
DisplayName();
DisplayAttributes();
DisplayVitals();
DisplaySkills();
}
private void DisplayName()
{
GUI.Label(new Rect(10, 10, 50, 25), "Name:" );
_toon.Name = GUI.TextArea(new Rect(65, 10, 120, 25), _toon.Name);
}
private void DisplayAttributes()
{
for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++)
{
GUI.Label(new Rect(10, 40 + (cnt * 25), 100, 25), ((AttributeName)cnt).ToString() );
GUI.Label(new Rect(115, 40 + (cnt * 25), 30, 25), _toon.GetPrimaryAttribute(cnt).AdjustedBaseValue.ToString() );
}
}
private void DisplayVitals()
{
for(int cnt = 0; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++)
{
GUI.Label(new Rect(10, 40 + ((cnt + 7) * 25), 100, 25), ((VitalName)cnt).ToString() );
GUI.Label(new Rect(115, 40 + ((cnt + 7) * 25), 30, 25), _toon.GetVital(cnt).AdjustedBaseValue.ToString() );
}
}
private void DisplaySkills()
{
for(int cnt = 0; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++)
{
GUI.Label(new Rect(150, 40 + (cnt * 25), 100, 25), ((SkillName)cnt).ToString() );
GUI.Label(new Rect(255, 40 + (cnt * 25), 30, 25), _toon.GetSkill(cnt).AdjustedBaseValue.ToString() );
}
}
}
And this is the class where I get my SkillName and VitalName.
using UnityEngine;
using System.Collections;
using System; //added to access the enum class
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.GetNames(typeof(AttributeName)).Length];
_vital = new Vital[Enum.GetValues(typeof(VitalName)).Length];
_skill = new Skill[Enum.GetValues(typeof(SkillName)).Length];
SetupPrimaryAttribute();
SetupVitals();
SetupSkills();
}
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();
}
//take average of all of the player skills and assign that as the player level.
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++)
{
_primaryAttribute[cnt] = new Attribute();
}
}
private void SetupSkills()
{
for(int cnt = 0; cnt < _skill.Length; cnt++)
{
_primaryAttribute[cnt] = new Attribute();
}
}
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 SetupVitalModifiers()
{
//health
GetVital((int)VitalName.Health).AddModifer(new ModifyingAttribute{attribute = GetPrimaryAttribute((int)AttributeName.Wisdom), ratio = 0.5f});
//stamina
GetVital((int)VitalName.Stamina).AddModifer(new ModifyingAttribute{attribute = GetPrimaryAttribute((int)AttributeName.Strength), ratio = 1});
GetVital((int)VitalName.Stamina).AddModifer(new ModifyingAttribute{attribute = GetPrimaryAttribute((int)AttributeName.Speed), ratio = 1});
//mana
GetVital((int)VitalName.Mana).AddModifer(new ModifyingAttribute{attribute = GetPrimaryAttribute((int)AttributeName.Knowledge), ratio = 1});
GetVital((int)VitalName.Mana).AddModifer(new ModifyingAttribute{attribute = GetPrimaryAttribute((int)AttributeName.Wisdom), ratio = 1});
GetVital((int)VitalName.Mana).AddModifer(new ModifyingAttribute{attribute = GetPrimaryAttribute((int)AttributeName.Concentration), ratio = 1});
}
private void SetupSkillModifiers()
{
//melee offence
GetSkill((int)SkillName.Melee_Offence).AddModifer(new ModifyingAttribute{attribute = GetPrimaryAttribute((int)AttributeName.Strength), ratio = 0.33f});
//melee defence
GetSkill((int)SkillName.Melee_Defence).AddModifer(new ModifyingAttribute{attribute = GetPrimaryAttribute((int)AttributeName.Speed), ratio = 0.33f});
//magic offence
GetSkill((int)SkillName.Magic_Offense).AddModifer(new ModifyingAttribute{attribute = GetPrimaryAttribute((int)AttributeName.Wisdom), ratio = 0.33f});
GetSkill((int)SkillName.Magic_Offense).AddModifer(new ModifyingAttribute{attribute = GetPrimaryAttribute((int)AttributeName.Concentration), ratio = 0.33f});
//magic defence
GetSkill((int)SkillName.Magic_Defense).AddModifer(new ModifyingAttribute{attribute = GetPrimaryAttribute((int)AttributeName.Knowledge), ratio = 0.33f});
//ranged offence
GetSkill((int)SkillName.Ranged_Offence).AddModifer(new ModifyingAttribute{attribute = GetPrimaryAttribute((int)AttributeName.Speed), ratio = 0.33f});
GetSkill((int)SkillName.Ranged_Offence).AddModifer(new ModifyingAttribute{attribute = GetPrimaryAttribute((int)AttributeName.Concentration), ratio = 0.33f});
//ranged defence
GetSkill((int)SkillName.Ranged_Defence).AddModifer(new ModifyingAttribute{attribute = GetPrimaryAttribute((int)AttributeName.Speed), ratio = 0.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();
}
}
}
Your code paste is jacked. Those line numbers mean nothing if you don't align your code with the code box.
SetupVitals()
sets values of the _primaryAttribute
array, rather than the _vital
array. (why can I not add this as an answer? I want rep) :(
Can you elaborate a bit more? Sorry I'm a bit confused.
Answer by BlueRaja_2014 · Jun 26, 2013 at 12:42 AM
private void SetupVitals()
{
for(int cnt = 0; cnt < _vital.Length; cnt++)
{
_primaryAttribute[cnt] = new Attribute();
}
}
SetupVitals()
should presumably be setting the values of the _vital
array, rather than the _primaryAttribute
array (which is already set in SetupPrimaryAttribute()
)
when you post the answer, it says 'your post is awaiting moderation'
your post is then actually awaiting moderation :P
good answer, have a plus
Ok I get whats the problem but I'm not 100% sure what code I should add. Sorry I just started c# I've been using c++ for while.
@Loius: Ah, thanks! It definitely did not say "Your post is awaiting moderation" (I still have the page open, I can post a screenshot if you'd like) - it simply brought me back to exactly the same page.
I also cannot post a question - there is no preview, nor a "post question" button. I tried on multiple browsers/computers, logged in and logged out. Do I need a certain amount of rep to ask a question, or is asking questions broken?
What? Are you talking to Loius or me? That doesn't help me in anyway.
@radsherwin: The code/problem/solution would apply equally to C++ and C#. Replace _primaryAttribute
with _vital
.
Is this copy+paste code? You should make an attempt to understand any code before copy+pasting it...
Follow this Question
Related Questions
BurgZergArcade tutorial 20 errors? 0 Answers
Burgzergarcade Why am i getting an error after re-importing? 1 Answer
Multiple Cars not working 1 Answer
What does "The type or namespace name x could not be found" mean? 5 Answers
error CS0103: The name `AttributeName' does not exist in the current context 0 Answers