- Home /
What am I doing wrong with my life script?
Hi. I am making a first person shooter and want to be able to make the player have life. The script should work, but it doesn't. I can't figure out what I am doing wrong.
var Life : int = 0; var GameOverLevel : String; var Damage = 5; var EnemyProjectile : Transform;
function OnCollisionEnter(myCollider : Collider) { if(myCollider.gameObject.name == (EnemyProjectile)) { Life -= Damage; } } function OnGUI() { GUI.Label (Rect (25, 25, 100, 30), "Life: "+Life); }
function update() {
if (Life == 0) { Application.LoadLevel (GameOverLevel); } }
also, if that's not the case, how is your script not working?
You also didn't comPlete your updat function just put two brackets at the end of the script. }}
Answer by Jesus_Freak · Dec 24, 2010 at 07:57 PM
you're saying to automatically game-over since you're lives are zero already. just make your lives 10 or something.
Edit: in the oncollisionenter function you need to make the parameter. :Collision, not :Collider for it to work. Make sure an object is a rigidbody for that function to even work!
also, if the problem is that the level isn't loading, you didn't add it to the build settings menu. file > build settings, just drag your scene file to the build settings, and to open it from the game,
Application.LoadLevel("GameOverLevel");
//just make sure the name of your level is the same as you wrote it in the LoadLevel script.
Your answer
![](https://koobas.hobune.stream/wayback/20220613092501im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
insert semicolon 2 Answers
How to deplete health every second 2 Answers
Destroy 1 prefab object and changing the color/image 1 Answer
health regeneration after death 1 Answer