- Home /
Pick up objects
In my 2D Game my player can lose and pick up lives. at this point, i can endlessly add lives, by bumping in to my life-object. So i want the object to disappear when i take it.
How do i do this?
if(hit.gameObject.tag == "dead") { dead = true; HealthControl.LIVES -= 1; }
if(hit.gameObject.tag == "life") { Destroy(life); HealthControl.LIVES += 1;}
what does 'lives' stand for? and what kind of variable is healthcontrol?
Answer by Lovrenc · Dec 29, 2012 at 05:33 PM
Even though your code should work i think this is not a good practice. Your player script seems to know about all the events and items in your game.
Suppose you want to add some other "potions and buffs". This would in your case also mean more tags and matching etc.
In your player script you should make a function:
void AddLife() {
`HealthControl.LIVES++;`
}
And add another script on your buff item:
void OnCollisionEnter(Collision collision) {
`if(collision.gameObject.Tag == "Player") {`
`collision.gameObject.SendMessage("AddLife")`
`Destroy(gameObject)`
`}`
}
All you have to do is:
function OnCollisionEnter(collision: Collision)
and
>`function AddLife()`
I think everything else stays the same.
Answer by Tijs · Jan 04, 2013 at 07:28 PM
I got it like this, thnx:)
var triggered: boolean;
function OnTriggerEnter() {
triggered = true;
}
function Update() {
if (triggered) {
Debug.Log("You have picked up a life");
Destroy(gameObject);
HealthControl.LIVES++;
}
}
Your answer
Follow this Question
Related Questions
Trying to Destroy Object when Wave starts 1 Answer
PlayerPref or DontDestroyOnLoad for lives 3 Answers
how to save destroyed state of an object 1 Answer
Can't destroy an arrray with GUI Text 1 Answer
[CLOSED] Render priority of gameObjects 0 Answers