- Home /
Lives dont minus correctly
My issue is that I am using the script so that when the object i place it on (an invisable box) is collided with it respawns you back at start and takes away a life. Problem comes in that when you have 6 boxes in game with the script on each you can hit each box 3 times before you die, not 3 life in general. I want it so that if u hit any of the boxes it removes a life.
var spawnPoint : Transform;
//var theShadow : GameObject;
public var life : int = 3;
function OnTriggerEnter (other : Collider)
{
other.gameObject.active = false;
//theShadow.active = false;
yield new WaitForSeconds (0.5);
other.transform.position = spawnPoint.position;
life -= 1;
if(life == 0) Application.LoadLevel("End Game");
other.gameObject.active = true;
// theShadow.active = true;
}
function OnGUI(){
GUI.Label (Rect (300, 100, 200, 30), "Life: " +life);
}
Answer by syclamoth · Dec 03, 2011 at 06:12 AM
The problem here is that the box itself is holding the 'lives' value, not the player! You need to split this functionality off into two different scripts. So, on your box script, do this-
function OnTriggerEnter (other : Collider)
{
other.SendMessage("Die", SendMessageOptions.DontRequireReceiver);
}
Then on your player, do this-
var spawnPoint : Transform; //var theShadow : GameObject; public var life : int = 3;
function Die() { life -= 1; if(life == 0) { Application.LoadLevel("End Game"); } else { StartCoroutine(DeactivateRespawn()); } }
function DeactivateRespawn () { gameObject.active = false; yield new WaitForSeconds (0.5); transform.position = spawnPoint.position; gameObject.active = true; }
function OnGUI(){ GUI.Label (Rect (300, 100, 200, 30), "Life: " +life); }
This way, the box tells the player to die and respawn, instead of the box doing the job itself. The player should know how to do this job better than a box should.
I guess in not doing somthing exactly correctly im geting a message BCE005: $$anonymous$$ identifier:"$$anonymous$$essageOptions". not quite sure how to fix this im a little new to scripting.
The correct parameter is Send$$anonymous$$essageOptions.DontRequireReceiver (who could memorize such a long name?)
Oops! I made a few mistakes there. Serves me right for coding when I'm tired!
The first one, aldo found (thanks!), the second one was that in StartCoroutine, I needed the () after the DeactivateRespawan function name!
I've updated my answer with the correct code for you.
"No appropriate version of 'UnityEngine.$$anonymous$$onoBehaviour.StartCoroutine' for the argument list '(function(): System.Collections.IEnumerator)' was found."
Translating this into english: The error is happening when you call "StartCoroutine," and the problem is the parameters (arguments) you passed it were not what it expected.
StartCoroutine actually takes an IEnumerator - the return value of a coroutine - as a parameter. The error messages says you're passing in a function rather than an enumerator.
Counter-intuitive as it might seem, if you're not using the string versions of coroutine (StartCoroutine("$$anonymous$$yFunctionName"), you don't pass it just the function, you actually call the coroutine function and pass the resulting IEnumerator into StartCoroutine.
A look at the line in question shows you're not calling the coroutine, just passing it's function...
//just passing the coroutine without calling it gives this error
StartCoroutine(DeactivateRespawn);
What you want to do is call the coroutine as the parameter, like this
//Calling it actually creates the coroutine and returns an IEnumerator,
//which is passed to StartCoroutine
StartCoroutine(DeactivateRespawn());
to help understand what's really going on, you could also write it this way:
//calling the coroutine doesn't run it, it just CREATES it; think of it
//as kindof like calling a constructor to make an instance of the coroutine
var coroutineEnumerator : IEnumerator = DeactivateRespawn( );
//StartCoroutine is what actually tells the coroutine instance you created
//to start running, adding it to a queue with other coroutines to run later
StartCoroutine( coroutineEnumerator );
Unityscript's syntax really obfuscates what's going on here, so I hope this explanation helps you understand coroutines and StartCoroutine a bit better!
If that all just confused you, here's the tl;dr version: just change the line with StartCoroutine to this:
StartCoroutine(DeactivateRespawn());
I got a little crazy there, but while I'd been using coroutines for a while, I'd just a couple of days earlier had the epiphany that made the coroutine syntax actually make sense in my head, and that was the first opportunity to share that revelation with the world, haha.
Your answer
Follow this Question
Related Questions
Lives bring down too fast 1 Answer
Lives aren't subtracting, going straight to game over. 2 Answers
How to slowly decrease health script? 3 Answers
Load level with lives based on score 1 Answer