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 /
This question was closed Jul 21, 2015 at 08:53 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by radsherwin · Jun 25, 2013 at 10:51 PM · c#errorburgzergarcade

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();    
         }
     }
 }
 
 
Comment
Add comment · Show 4
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 darthbator · Jun 26, 2013 at 12:11 AM 0
Share

Your code paste is jacked. Those line numbers mean nothing if you don't align your code with the code box.

avatar image radsherwin · Jun 26, 2013 at 12:13 AM 0
Share

I Updated it!

avatar image BlueRaja_2014 · Jun 26, 2013 at 12:27 AM 0
Share

SetupVitals() sets values of the _primaryAttribute array, rather than the _vital array. (why can I not add this as an answer? I want rep) :(

avatar image radsherwin · Jun 26, 2013 at 12:28 AM 0
Share

Can you elaborate a bit more? Sorry I'm a bit confused.

1 Reply

  • Sort: 
avatar image
3
Best Answer

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

Comment
Add comment · Show 6 · 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 Loius · Jun 26, 2013 at 12:43 AM 0
Share

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

avatar image radsherwin · Jun 26, 2013 at 12:45 AM 0
Share

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.

avatar image BlueRaja_2014 · Jun 26, 2013 at 12:50 AM 0
Share

@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?

avatar image radsherwin · Jun 26, 2013 at 12:53 AM 0
Share

What? Are you talking to Loius or me? That doesn't help me in anyway.

avatar image BlueRaja_2014 · Jun 26, 2013 at 12:58 AM 0
Share

@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...

Show more comments

Follow this Question

Answers Answers and Comments

17 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 avatar image avatar image

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


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