- Home /
Trying to create a gameover trigger with enemy.
I'm trying to make a gameover trigger when an enemy touches me, but I'm having problems with the code. The errors I have are:
error CS1525: Unexpected symbol ` whatHitMe' error CS8025: Parsing error
Here's my coding. I have two scripts for the game and the gameover trigger:
using UnityEngine;
using System.Collections;
public class SpaceGameManager : MonoBehaviour {
private string gameState;
public GUIStyle myTextStyle;
void Awake()
{
gameState = "playing";
}
public void SetGameOver()
{
Time.timeScale = 0.0f; // pause game
gameState = "lost";
}
void OnGUI()
{
if (gameState == "lost")
{
GUILayout.Label("You Lost!", myTextStyle);
if (GUILayout.Button("Try Again"))
{
Application.LoadLevel(Application.loadedLevel);
}
}
}
using UnityEngine;
using System.Collections;
public class SpaceGameoverTrigger : MonoBehaviour {
SpaceGameManager SGM;
// Use this for initialization
void Start () {
OnCollisionEnter(Collision whatHitMe)
{
if(whatHitMe.gameObject.tag == "Enemy")
{
ccs.FoundCoin();
Destroy(whatHitMe.gameObject);
}
}
SGM = whatHitMe.gameObject.tag == "Enemy";
}
void OnTriggerEnter()
{
SGM.SetGameOver();
}
// Update is called once per frame
void Update () {
}
}
Thanks in advance!
Answer by wijesijp · Apr 25, 2014 at 11:17 AM
your OnCollisionEnter function is inside start function. Move it out.
Done, but now I'm getting an error saying whatHit$$anonymous$$e does not exist in the current context.
using UnityEngine;
using System.Collections;
public class SpaceGameoverTrigger : $$anonymous$$onoBehaviour {
SpaceGame$$anonymous$$anager SG$$anonymous$$;
void OnCollisionEnter(Collision whatHit$$anonymous$$e)
{
if(whatHit$$anonymous$$e.gameObject.tag == "Enemy")
{
Destroy(whatHit$$anonymous$$e.gameObject);
}
}
// Use this for initialization
void Start () {
SG$$anonymous$$ = whatHit$$anonymous$$e.gameObject.tag == "Enemy";
}
void OnTriggerEnter()
{
SG$$anonymous$$.SetGameOver();
}
// Update is called once per frame
void Update () {
}
}
It's probably something simple that I'm missing but I can't figure this out.
you still have SG$$anonymous$$ = whatHit$$anonymous$$e.gameObject.tag == "Enemy";
inside the Start function. It should be inside the OnCollisionEnter function
Okay, now it's saying it cannot implicitly convert type bool' to
SpaceGame$$anonymous$$anager'.
whatHit$$anonymous$$e.gameObject.tag == "Enemy"; return true or false
so SpaceGame$$anonymous$$anager type SG$$anonymous$$ can't be equal to that. I guess what needs to happen is ...
void OnCollisionEnter(Collision whatHit$$anonymous$$e)
{
if(whatHit$$anonymous$$e.gameObject.tag == "Enemy")
{
// do some other action with SG$$anonymous$$
Destroy(whatHit$$anonymous$$e.gameObject);
}
}
I can't think of another action to use it with. What would you suggest?