- Home /
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);
}
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.
@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).
Your answer
Follow this Question
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