These past weeks I have been working on moving the enemy and making the enemy attack with simple if-else statements and it has been glorious. While on my programming streak, I decided “Hey let’s move from working on 1 enemy to multiple enemies!” This seemed like the logical next step and I was happy enough with my progress to move on. I started this process a few days ago and I was raring to go and started coding away.
However, a blight came. When the second enemy was trying to move, the first one would move again even though it’s turn had been and passed. For two days straight I looked over my code and worked my way through it with many Debug.Log’s. The error of my coding ways evaded me.
Up until today I feared I wouldn’t progress, that my schedule was going to fall to pieces. I couldn’t have this. One more time I looked through my code, through every statement, through every case. I found the problem at long last.
Each enemy has a Controller script which contains the following:
StateMachine state = FindObjectOfType<StateMachine>();
if(state.has_moved == false && state.cur_state == States.ENEMY)
I only ever have one StateMachine in the game and all enemies are relying off this one script. There is no distinguishable variable in the if statement that separates enemy 1 from enemy 2.
The solution was easy, add a variable that the Controller script could access that would be distinguishable. Hey presto, or hey boolio, I solved the problem!
Now I have a player that can attack and kill things and multiple enemies that move around and attack! The next step for me is to have some way for the player to choose which enemy to attack!
Stay tuned for more FYP updates!