Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by AdminBenni · Apr 14, 2017 at 03:27 PM · c#nullreferenceexceptionclass library

Unexpected NullReferenceException

I am working on a game using a class library I made (Soldiers.dll) where I can place a SoldierObject script on a GameObject to make it behave like a soldier. However, I have had constant problems with an unexpected NullReferenceException and I quite frankly cannot understand what's going on here.

Here is the setup of my scene.

I have two GameObjects: Soldier1 and Soldier2 each having a SoldierObject script on them: alt text The SoldierObject script looks like this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using Soldiers;
 
 public class SoldierObject : MonoBehaviour
 {
 
     public WeaponObject weapon;
     public ArmorObject armor;
     public string Name = "Unknown";
     public int Hp = 1;
     public uint Strength = 0;
     public uint Agility = 0;
 
     public Soldier soldier;
 
     void OnEnable()
     {
         soldier = new Soldier(Name, weapon.weapon, armor.armor, Hp, Strength, Agility);
     }
 
     void Update ()
     {
         Name = soldier.name();
         weapon.weapon = soldier.wep();
         armor.armor = soldier.arm();
         Hp = soldier.hp();
         Strength = soldier.strength();
         Agility = soldier.agi();
     }
 }
 

So far everything works. However, things start going awry when the AttackOther script comes into play which is placed on Soldier1:

alt text The script looks like this:

 using System.Collections;
 using System.Collections.Generic;
 using System;
 using UnityEngine;
 using Soldiers;
 
 public class AttackOther : MonoBehaviour {
 
     public SoldierObject other;
     public SoldierObject self;
 
     private bool attacked = false;
     
     // Update is called once per frame
     void Update () {
         if(Time.time >= 3 && !attacked)
         {
             attacked = true;
             self.soldier.attack(other.soldier, 1);
         }
     }
 }

The attack function is a public function of the Soldier class:

 public SoldierState attack(Soldier other, double mult)
         {
             int atk = (int)Math.Round(this.wep().power() * mult) + (int)this.strength();
             return other.hp((int)(other.hp() - Math.Abs(other.arm().defense() - atk))) > 0 ? other.state : SoldierState.dead;
         }

At this time whenever

 self.soldier.attack(other.soldier, 1);

is executed I get this error:

NullReferenceException: Object reference not set to an instance of an object

Soldiers.Soldier.attack (Soldiers.Soldier other, Double mult) (at C:/Users/AdminBenni/documents/visual studio 2017/Projects/Soldiers/Soldiers/Class1.cs:160)

AttackOther.Update () (at Assets/Scripts/AttackOther.cs:19)

This makes me confused because as far as I know everything I'm referencing actually exists.

All help appreciated :)

temp2.png (11.0 kB)
temp3.png (7.4 kB)
Comment
Add comment · Show 1
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 Commoble · Apr 14, 2017 at 03:56 PM 0
Share

To solve null reference exceptions, you should find the line that's causing it (line 160 in your class1.cs script, which either has an incorrect filename or you haven't shown it to us), then deter$$anonymous$$e which field you're referencing is null when it shouldn't be, then figure out where, why, and when the field is being set to null. Then, you can either fix what's causing it to be null, or go back to the line that throws the error and handle what should happen when that field is null.

As for deter$$anonymous$$ing which field is null:

as far as I know everything I'm referencing actually exists

If everything you're referencing actually existed, you wouldn't get a null reference error, so "As far as I know" isn't enough to help you here. Use things like this to figure out what's null when it shouldn't be:

 if (somefield == null)
 {
     Debug.Log("Somefield is null!");
 }

0 Replies

· Add your reply
  • Sort: 

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Having a issue with a NullReferenceException 1 Answer

Property Drawer Doesn't Show Property 1 Answer

Attacking And Health Script in C# Gives Error : NullReferenceException: Object reference not set to an instance of an object 0 Answers

Null Reference Exception on Sprite Renderer 0 Answers

Random Chance NullReferenceException: Object reference not set to an instance of an object 2 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