Game Over when sprite hits you.

Hey,
My Unity Answers Question got rejected :rage:. I really need help so I came here. Here is the original question;

How to die when a sprite hits you?
0

Help!

I’m making a 3D game where a sprite chases you (Like Slender) When the sprite hits you, It sends you to the game over screen. But it doesn’t. It just goes through you. Can anyone help? I’ve tried all other things I could find, but none worked. If you need more info, just ask.

Code

  • #pragma strict
  • functionOnCollisionEnter(other :Collision){
  • Application.LoadLevel(“gameover”);
  • }

I found this code online. It doesn’t make any errors in the console.

Video:

Hm, that’s odd.
When I make a sprite hit you, it always sends me to game over. You should double check your sprite hits you script.

Do both objects have a Rigidbody component?

1 Like

Also, I think there’s a typo, there should be a space between function and OnCollisionEnter. Like this:

function OnCollisionEnter() {}

Hey,
I’ve tried all of the suggestions and they haven’t worked.:face_with_spiral_eyes:
They both have a rigidbody component. What needs to be ticked /selected in the rigidbody component.
( I’m a Unity noob…):hushed:

Do they have colliders?

yeah they both have box coliders

And do either have the script attached? Can you post the code? (Use [ code ] [ /code ], without the spaces)

do you mean the code when you hit the enemy or some other one?

I mean the script you use to recognize a collision. Post it. Also, does this script exist on at least one of the objects colliding.

  • #pragma strict
  • functionOnCollisionEnter(other :Collision){
  • Application.LoadLevel(“gameover”);
  • }

I have it on the enemy

Is there a space there, between function and onCollisinEnter()?

yeah.
i have an updated script here:
function OnCollisionEnter(hit : Collision){
Application.LoadLevel(“gameover”);
}

Ok, first off. When posting code, do as I asked. Post it in code tags, it makes it easier to read.

function someDopeAssFunction(){
doSomething();
}

Second, it could be many things.
A. Your objects don’t have colliders or rigidbodies. You say they do tho. Check.
B. your objects don’t have the script attached. Check.
C. No level called game over exists.

Finding the reason for an error, of whatever sort, takes detective work.
Good luck Sherlock!

Thanks, i’ll figure it out eventually. ;D

What I do often when trying to isolate a problem is use prints. I place a print to the console at places that may lead me to the issue.

print("is this thing on? ");

Also, since you are using collisions make sure the colliders are not triggers.

function OnCollisionEnter (hit : Collision){
      print("is this thing on? ");
        Application.LoadLevel("gameover");
}

This is the code.
No text comes up in the console.
I have no idea whats wrong :face_with_spiral_eyes:

That’s obvious because you simply declared a hit variable and did nothing to it!

  1. Attach a tag to your player called “Player”
  2. Change your script to this:
function OnCollisionEnter (hit : Collision){
if (hit.gameObject == "Player") {
print("is this thing on? ");
Application.LoadLevel("gameover");
}
}

Jeez. If this doesn’t work then go to learn section!

Actually, with the triggers issue, here’s how I have approached a problem like this:

  • First, is scene actually in the build settings? if not, then it will definitely fail.
  • Are the tags set right? I often got problems in projects where a new prefab, such as a new car, wouldn’t register the right things, of which was caused by my forgetfulness, i.e. forgot to tag the car, “car”
  • The way collisions work: collider->collider, trigger->collider, trigger->trigger
  • But trigger->collider registers nothing…

…so, try attaching the script to the player instead, make sure the enemies aren’t triggers (same with the player) and make sure the player script checks the tags o see if the collider is tagged “enemy.” also, check that all objects are tagged appropriately.

Good luck! :slight_smile:

Place a Box Collider on your sprite component, make it a little bigger then your sprite… and set its IsTrigger flag.
Then see if your code works.