- Home /
C# Enemy dies on Collision
I'm making a game wherein the purpose is to push an opponent into a pit in order to win, there's a script in place on the player character, that means that if it falls in the pit, you get a game over. But I'm not sure how I should make one for the Enemy, so that if the enemy falls in the pit you'll get a 'You Win'
using UnityEngine;
public class RobotCollision : MonoBehaviour {
public MoveScript movement;
void OnCollisionEnter(Collision collisionInfo)
{
Debug.Log(collisionInfo.collider.name);
if (collisionInfo.collider.tag == "Obstacle")
{
movement.enabled = false;
FindObjectOfType<GameManager>().GameOver();
}
}
}
This is the player collision script that I'm using to deliver the Game Over. I figured that I could reuse this script for the enemy with some slight modifications. Which lead to this script:
using UnityEngine;
using System.Collections;
public class EnemyCollision : MonoBehaviour
{
void OnCollisionEnter(Collision collisionInfo)
{
Debug.Log(collisionInfo.collider.name);
if (collisionInfo.collider.tag == "Obstacle")
{
FindObjectOfType<GameManager>().YouWin();
}
}
}
Unfortunately this threw up an error on the final line at .YouWin();
I reckoned this was a simple fix by going into my Game Manager and adding a new public void called YouWin() but unfortunatley, this created more errors. I'm not very knowledgable in scripting, so any help will be much appreciated.
No definition for YouWin(), which is what lead my to the Game $$anonymous$$anager to create a new Public Void. Adding a public void YouWin() just under public void GameOver() leads Visual Studio to say that GameOver() must declare a body. Then the public on the YouWin() is underlined in red and says it expects a ;
Give us your Game$$anonymous$$anager code as well.
Answer by SlowCircuit · Jun 18, 2018 at 02:07 AM
Your YouWin() method is inside the GameOver() method before even the brackets. You just put it in the completely wrong spot. Every method must have braces after it like this:
public void GameOver() { }
public void YouWin() { }
So your code should look something more like:
public void GameOver() {
if (gameHasEnded == false) {
gameHasEnded = true;
Debug.Log("GAME OVER");
Invoke("Restart", restartDelay);
}
}
public void YouWin() {
if (gameHasEnded == false) {
gameHasEnded = true;
Debug.Log("PLAYER WON");
Invoke("Restart", restartDelay);
}
}