In that long post I did above which had all the steps required to get where I had got to, I had missed something.
I was just looking at your CollisionHandler.cs script and note that you’ve added this;
private GameController gameController;
private void Start()
{
gameController = FindObjectOfType<GameController>();
}
…but nothing else in that script uses it.
Looking at my instructions above, I indicated to make these changes and then moved to the GameController.cs script where we create the GameOver
method - the CollisionHandler.cs script was then meant to call that method but it would seem I forgot to add that instruction for you - my apologies.
So, in your CollisionHandler.cs_ script, make this change;
void OnTriggerEnter(Collider other)
{
StartDeathSequence();
deathFX.SetActive(true);
gameController.GameOver();
}
You can now see the call to the GameOver
method.
Make this one change and you should find that when you crash into an enemy the player ship disappears, the movement along the rail stops, and the “Game Over!” text appears.
Do this before we move on as that will save creating a huge post with multiple instructions.
Also, this code;
void Update()
{
GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemies");
enemiesLeft = enemies.Length;
if (Input.GetKeyDown(KeyCode.A))
{
enemiesLeft--;
}
if (enemiesLeft == 0)
{
endGame();
}
}
in your GameController.cs isn’t going to do what you want it to. Update
is called every frame, but above your keyboard input check you are getting a fresh count of how many enemies you have left, so whilst you deduct one when your press A, the next frame it gets a fresh count to corrects it again.
I’m guessing you are using a GamePad when you play, as an aside, you have the KeyCode.A for this check which is also the same key to move the ship to the left
I would just delete this for now;
if (Input.GetKeyDown(KeyCode.A))
{
enemiesLeft--;
}
Let me know when you’re ready and we’ll move on to the next bit.