life is null
can you give me a code. i have 3 lives but when i click wrong button continuously it shows that life turns to negative ( example. -1, -2....) can you please help me on my code?
Answer by FeeeshMeister · Aug 05, 2016 at 03:54 AM
Here is what I think you want
If you want to display some kind of Game Over screen when lives = 0 it is this simple, change this at the beginning of the script.
using UnityEngine;
using System.Collections;
to this
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
then do this.
void OnClickWrong(){
life -= 1;
}
void Update () {
if (life == 0)
SceneManager.LoadScene (sceneindex); //replace scene index with the game over scene's index
}
however, if you don't want there to be a game over, there is a way to simply keep it from going below 0, by clamping it. Add this to OnClickWrong.
void OnClickWrong(){
life -= 1;
life = Mathf.Clamp(life, 0f, Mathf.Infinity);
}
That should just stop it from going below 0, if neither of these are what you wanted, just comment and I will do my best to answer. All code is untested, but should work.
Answer by 5c4r3cr0w · Aug 03, 2016 at 12:07 PM
void OnClickWrong(){
life -= (life > 0) : 1:0;
}
It's just a pseudo code.
Your answer
Follow this Question
Related Questions
Is it better to come in this field as a designer or an engineer?? 0 Answers
Enemy is loosing life when he's gun is shot 0 Answers
Ennemy don't lose life [C#] 1 Answer
Adding to variable only once? 0 Answers
Help with Collider? 2 Answers