Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Jase of Dragons · May 14, 2013 at 04:24 AM · errornullreferenceexceptionnull reference exceptionnull reference

Null Reference Exception Error. Modified Stats. Need Help.

All right, having trouble with null reference exceptions.....again. So I decided to make an account on Unity in hopes that one of your gifted scripters can help an apprentice such as myself. Here is the error:

NullReferenceException: Object reference not set to an instance of an object ModifiedStats.CalculateModValue () (at Assets/Scripts/Character Classes/ModifiedStats.cs:19) ModifiedStats.Update () (at Assets/Scripts/Character Classes/ModifiedStats.cs:29) BaseCharacterClass.StatsUpdate () (at Assets/Scripts/Character Classes/BaseCharacterClass.cs:193) CharacterGenerator.Start () (at Assets/Scripts/Character Classes/CharacterGenerator.cs:26)

Some scripts to aid your assistance:

 using System.Collections.Generic;
 public class ModifiedStats : BaseStats {
     private List<ModifyingAttribute> _mods; //List of Attributes that Modify Stat
     private int _modValue; //amount added to baseValue of modifiers
     
     public ModifiedStats() {
         _mods = new List<ModifyingAttribute>();
         _modValue = 0;
     }
     
     public void AddModifier( ModifyingAttribute mod) {
         _mods.Add(mod);
     }
     
     private void CalculateModValue() {
         
         if(_mods.Count > 0 )
             foreach (ModifyingAttribute att in _mods) 
                 _modValue += (int)(att.attribute.AdjustedBaseValue * att.ratio);  
         
         
     }
     
     public new int AdjustedBaseValue {
         get{ return _baseValue + _buffValue + _modValue; }
     }
     
     public void Update() {
         CalculateModValue();
     }
 }
 
 public struct ModifyingAttribute {
     public Attribute attribute; 
     public float ratio; 
     
 }


Another one:

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class BaseCharacterClass : 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]; 
         
         SetupVitals();
         SetupPrimaryAttribute();
         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;
         
     CaluclateLevel();
         
     }
     // add more to this one, incomplete
     //take average of all players skills and assign that as the player level
         
     public void CaluclateLevel() {
     }
     
     public void SetupPrimaryAttribute() {
         for(int cnt = 0; cnt < _primaryAttribute.Length; cnt ++)
             _primaryAttribute[cnt] = new Attribute();
         
     }
     
     public void SetupVitals() {
                 for(int cnt = 0; cnt < _vital.Length; cnt ++)
             _vital[cnt] = new Vital();
         
         SetUpVitalModifiers();
 
 
         
         
     }
     
     public void SetupSkills() {
             for(int cnt = 0; cnt < _skill.Length; cnt ++) 
             _skill[cnt] = new Skill();
         
         SetUpSkillModifiers();
         
 
         
         
     }
     public Attribute GetPrimaryAttribute(int index) {
         return _primaryAttribute[index]; 
     }
     
     public Vital GetVital(int index) {
         return _vital[index];
     }
     public Skill GetSkills(int index) {
         return _skill[index];
     }
     private void SetUpVitalModifiers(){
         //health
         GetVital((int)VitalName.Vitality).AddModifier(new ModifyingAttribute{attribute = GetPrimaryAttribute((int)AttributeName.Physique), ratio = .5f});
         
         //energy
         ModifyingAttribute energyModifier = new ModifyingAttribute();
         energyModifier.attribute = GetPrimaryAttribute((int)AttributeName.Willpower);
         energyModifier.ratio = 1; 
         
         GetVital ((int)VitalName.Energy).AddModifier(energyModifier);
         
         
         
     }
     private void SetUpSkillModifiers(){
         //physique
         ModifyingAttribute SwordsmanshipModifier = new ModifyingAttribute();
         ModifyingAttribute AxemanshipModifier = new ModifyingAttribute();
         ModifyingAttribute BlockModifier = new ModifyingAttribute();
         
         SwordsmanshipModifier.attribute = GetPrimaryAttribute((int)AttributeName.Physique);
         SwordsmanshipModifier.ratio = .33f;
         
         AxemanshipModifier.attribute = GetPrimaryAttribute((int)AttributeName.Physique);
         AxemanshipModifier.ratio = .33f;
         
         BlockModifier.attribute = GetPrimaryAttribute((int)AttributeName.Physique);
         BlockModifier.ratio = .33f;
 
         GetSkills ((int)SkillName.Swordsmanship).AddModifier(SwordsmanshipModifier);
         GetSkills ((int)SkillName.Axemanship).AddModifier(AxemanshipModifier);
         GetSkills ((int)SkillName.Block).AddModifier(BlockModifier);
         
         //finesse
         ModifyingAttribute DaggeryModifier = new ModifyingAttribute();
         ModifyingAttribute MarksmanshipModifier = new ModifyingAttribute();
         ModifyingAttribute TacticsModifier = new ModifyingAttribute();
         
         DaggeryModifier.attribute = GetPrimaryAttribute((int)AttributeName.Finesse);
         DaggeryModifier.ratio = .33f;
         
         MarksmanshipModifier.attribute = GetPrimaryAttribute((int)AttributeName.Finesse);
         MarksmanshipModifier.ratio = .33f;
         
         TacticsModifier.attribute = GetPrimaryAttribute((int)AttributeName.Finesse);
         TacticsModifier.ratio = .33f;
         
         GetSkills ((int)SkillName.Daggery).AddModifier(DaggeryModifier);
         GetSkills ((int)SkillName.Marksmanship).AddModifier(MarksmanshipModifier);
         GetSkills ((int)SkillName.Tactics).AddModifier(TacticsModifier);
         
         //lordcraft
         ModifyingAttribute ChaosModifier = new ModifyingAttribute();
         ModifyingAttribute LightModifier = new ModifyingAttribute();
         
         ChaosModifier.attribute = GetPrimaryAttribute((int)AttributeName.Lordcraft);
         ChaosModifier.ratio = .33f;
         
         LightModifier.attribute = GetPrimaryAttribute ((int)AttributeName.Lordcraft);
         LightModifier.ratio = .33f;
         
         GetSkills ((int)SkillName.Light_Prayers).AddModifier(LightModifier);
         GetSkills ((int)SkillName.Chaos_Prayers).AddModifier(ChaosModifier);
         
         //charisma
         
         ModifyingAttribute CoercionModifier = new ModifyingAttribute();
         ModifyingAttribute MercantileModifier = new ModifyingAttribute();
         
         CoercionModifier.attribute = GetPrimaryAttribute((int)AttributeName.Charisma);
         CoercionModifier.ratio = .33f;
         
         MercantileModifier.attribute = GetPrimaryAttribute((int)AttributeName.Charisma);
         MercantileModifier.ratio = .33f;
         
         GetSkills ((int)SkillName.Coercion).AddModifier(CoercionModifier);
         GetSkills ((int)SkillName.Mercantile).AddModifier(MercantileModifier);
         
         //willpower
         ModifyingAttribute NaturalModifier = new ModifyingAttribute();
         ModifyingAttribute DominateModifier = new ModifyingAttribute();
         
         NaturalModifier.attribute = GetPrimaryAttribute((int)AttributeName.Willpower);
         NaturalModifier.ratio = .33f;
         
         DominateModifier.attribute = GetPrimaryAttribute((int)AttributeName.Willpower);
         DominateModifier.ratio = .33f;
         
         GetSkills ((int)SkillName.Natural_Arts).AddModifier(NaturalModifier);
         GetSkills ((int)SkillName.Domination_Arts).AddModifier(DominateModifier);
         
     
     }
     
     public void StatsUpdate() {
         for(int cnt = 0; cnt < _vital.Length; cnt ++)
             _vital[cnt].Update();
         for(int cnt = 0; cnt < _skill.Length; cnt ++)
             _skill[cnt].Update();
     }
 }



Thank you for all of your help. I am very appreciative.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SubatomicHero · May 14, 2013 at 07:12 AM

I've got a feeling you need to initialise the values in your struct to some defaults like so:

 public struct ModifyingAttribute 
 {
     public Attribute attribute; 
     public float ratio; 
 
     // See if this works
     ModifyingAttribute(Attribute att = new Attribute(), float r = 0.0f)
     {
         attribute = att;
         ratio = r;
     }  
 }

I know this works in C++, not sure if it works in C#

Comment
Add comment · Show 4 · 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 Jase of Dragons · May 16, 2013 at 01:58 AM 0
Share

I tried that, but it didn't work.

Error: Assets/Scripts/Character Classes/$$anonymous$$odifiedStats.cs(37,60): error CS1736: The expression being assigned to optional parameter `att' must be a constant or default value

Really happy you tried to help me. $$anonymous$$eans a lot.

avatar image Jase of Dragons · May 16, 2013 at 02:27 AM 0
Share

Thanks for your help, but it does not seem to work. It just turns up another error:

Assets/Scripts/Character Classes/$$anonymous$$odifiedStats.cs(37,59): error CS1736: The expression being assigned to optional parameter `att' must be a constant or default value

avatar image Fornoreason1000 · May 16, 2013 at 02:32 AM 0
Share

yea don't use the constrctor optional params need to be compile time constant in C#. try this

 public struct $$anonymous$$odifyingAttribute 
 {
     public Attribute attribute = new Attribute(); 
     public float ratio = 0.0f; 
  
 
  
 }
avatar image Jase of Dragons · May 16, 2013 at 03:06 AM 0
Share

when I do that, I get two new errors:

Assets/Scripts/Character Classes/$$anonymous$$odifiedStats.cs(36,26): error CS0573: $$anonymous$$odifyingAttribute.attribute': Structs cannot have instance field initializers Assets/Scripts/Character Classes/$$anonymous$$odifiedStats.cs(37,22): error CS0573: $$anonymous$$odifyingAttribute.ratio': Structs cannot have instance field initializers

I think that you can't arrange it like that in a struct. Idk, but thanks for your help nonetheless.

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

15 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

Related Questions

NullReferenceException yet Debug.Log shows nothing is null? 1 Answer

NullReferenceException on empty project because of ScriptCompilerBase 0 Answers

Null Reference Exception, what is it and why do I get it? 1 Answer

I keep getting a null reference exception, can anyone help? 0 Answers

im getting this error: NullReferenceException: Object reference not set to an instance of an object Item.Start () (at Assets/Scripts/Inventory System/Item.js:9) 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