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 zpunehx · May 25, 2014 at 03:29 PM · c#nullreferenceexceptionstate-machineevent-listener

Subscribing to an event via non-mono script.

currently i am using the state pattern for AI in my beginner game. here is the steps:

1)i set the initial state of my buildings to idle.

2)idle has two major methods. register and unregister units.

3)register unit is just a counter that counts the number of ai tanks alive.

3)as long as it's less than 5 i delegate to a new state called "building recruiting;which basically recruits till i have 5 units then i go back to idle.

this is the vision i had in mind. how ever here is the problem.

problem:

i get this error (a null value was found when an instance of an object is required).i have been trying to debugg this for last 2-3 hours, i googled the error but not much explanation even on stack overflow.

brief explanation of my scripts:

my logic was that on the start of the TankAI i would fire an event that has the "register unit" method subscribed and the opposite for when it's destroyed.below i will post the BuildingAI script , the building_idle state script and the tank AI script. i am a beginner so i apologize upfront if it is a silly error but i am really lost here. thank you for your time.

TankAI Script

public static event System.Action tankDeath;

 public static event 
     System.Action tankCreation;


 private AIState _myState;
 private string _playerTag;
 private Tank _myTank;

 private void Start() {
     tankCreation ();
     _playerTag = "Player";
     _myTank = GetComponentInChildren<Tank> ();
     myAgent = GetComponentInChildren<NavMeshAgent> ();
     _myState = new Tank_Guard(this,_myTank,myAgent);
 }

private void OnDestroy() {

     tankDeath ();
 }



BuildingAI script

private AIState _myState;

 private Building _myBuilding;

 private void Start () {
     _myBuilding = GetComponent<Building> ();
     _myState = new Building_Idle (_myBuilding);
 }

 private void Update() {
     _myState.updateSate (this);
     //Debug.Log (_myState.UnRegiesterUnit);
 }

 public void SetState (AIState state) {
     _myState = state;
 }

 private void OnEnable() {
     TankAI.tankCreation += _myState.RegiesterUnit;
     TankAI.tankDeath += _myState.UnRegiesterUnit;
 }

 private void OnDisable() {
     TankAI.tankCreation -= _myState.RegiesterUnit;
     TankAI.tankDeath -= _myState.UnRegiesterUnit;
 }

}

and finally the building Idle state script

private int tankCount;

private Building _myBuilding;

public Building_Idle(Building myBuilding) {

     tankCount = 0;

     this._myBuilding = myBuilding;

}

 public override void updateSate (AI myAI) {
     ((BuildingAI)myAI).SetState (this);
     //Debug.Log ("hi");
 }

 public override void RegiesterUnit () {
     tankCount++;
 }

 public override void UnRegiesterUnit (){
     tankCount--;
     Debug.Log (tankCount);
 }
Comment
Add comment · Show 2
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 Jeff-Kesselman · May 25, 2014 at 03:38 PM 0
Share

Please format your code in code blocks so we can read it.

Also, when asking for help with an error message, give the entire error message as displayed by unity as there is important debugging information in it, such as where the error actually occurred.

avatar image zpunehx · May 25, 2014 at 05:13 PM 0
Share

@Jeff$$anonymous$$esselman I apologize for this upfront. the error message was located in the following places: Building AI Script lines (18,19,23,24).

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

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to check for null 2 Answers

NullRefenceException error 1 Answer

NullReferenceException for reasons I don't understand. 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