- Home /
Upgrades not applying after first death
Ok so I've got a space-shooter type game I've been making in my spare time, trying to learn how to use unity and script in js. Now I've recently run into an issue. I have a powerup that spawns from random dead enemies. I can pick it up, and it properly applies to my ship, enhancing the weapon. It has multiple upgrades based on the animation it shows when you pick it up, that works fine. However, after you die once, the powerups do not work. You can pick them up sure, but they aren't being applied to the player, they just vanish.
Not really sure where to look. I have a feeling I'm doing the respawn script wrong, it's making a new copy of the player and maybe it should somehow reuse the last instance of my player, how would I do that?
I can post some code segments, not sure where the culprit is so not really sure what to post.
Could we have a look at the following code segments: The place where the powerup is triggered, applied and deleted. The place where the ship is deleted when it dies. And The place where you respawn the ship. That would help a lot to try and find the problem.
Answer by ThePunisher · Nov 20, 2013 at 10:16 PM
Wait, I think I found it... So at start you get a reference to a GameObject tagged "wep_up":
upgrade = GameObject.FindGameObjectWithTag("wep_up").GetComponent(upgrade_script);
Then when you collide with it, you go ahead and destroy it at the very bottom:
if (col.gameObject.tag == "wep_up"){
if (upgrade.PowType == 1){
if (wep1 == false){
wep1 = true;
wep2 = false;
wep3 = false;
PowLvl = 1;
}
else{
PowLvl++;
}
}
if (upgrade.PowType == 2){
if (wep2 == false){
wep1 = false;
wep2 = true;
wep3 = false;
PowLvl = 1;
}
else{
PowLvl++;
}
}
if (upgrade.PowType == 3){
if (wep3 == false){
wep1 = false;
wep2 = false;
wep3 = true;
PowLvl = 1;
}
else{
PowLvl++;
}
}
Instantiate(shield_pref, Vector3(transform.position.x,transform.position.y, 2), Quaternion.identity);
shield_pref.particleSystem.Emit == true;
Destroy(col.gameObject);
}
But you never get another reference to the upgrade. Can place this code real quick right after the "wep_up" condition passes.
upgrade = col.gameObject.GetComponent(upgrade_script);
Woops. I was trying to turn my comment into an answer and accidentally deleted all our comments, lol. Can you please mark this as the answer if it solved the problem you were having?
Answer by asylum101 · Nov 14, 2013 at 11:30 PM
Yes, I instantiate the prefab. The new instance is as I want it, however as I said, it won't take the new powerups. Here are the scripts, I hope they're not too messy because I had to copy/paste everything into notepad because this site was bugging out for me.
The powerup spawns by chance when you kill enemies: (part of the laser_script, a component of the laser itself.)
if (col.gameObject.tag =="Enemy"){
manager.ShipsKilled++;
manager.points = manager.points + 100;
number = Random.Range(minRNG, maxRNG);
if (number == 1) {
number = Random.Range(minRNG, maxRNG);
if (number > 5){ //if roll is higher than 5, spawn shield, if less than, spawn wep powerup, min/max default is 1/10
Instantiate(shield_powerup,transform.position,Quaternion.identity);
}
if (number < 5){
Instantiate(wep_powerup,transform.position,Quaternion.identity);
}
}
Instantiate(enemydeath_pref, Vector3(transform.position.x,transform.position.y, 2), Quaternion.identity); //spawns particle emmiter on death
enemydeath_pref.particleSystem.Emit == true;
Destroy(col.gameObject); //destroy the enemy ship
if(player.wep2 == false){
Destroy(gameObject); // destroy the weapon particle if it's not wep2.
}
}
aaaand to apply/delete the power up: (this is a part of the playerscript, a component of the player)
if (col.gameObject.tag == "wep_up"){
if (upgrade.PowType == 1){
if (wep1 == false){
wep1 = true;
wep2 = false;
wep3 = false;
PowLvl = 1;
}
else{
PowLvl++;
}
}
if (upgrade.PowType == 2){
if (wep2 == false){
wep1 = false;
wep2 = true;
wep3 = false;
PowLvl = 1;
}
else{
PowLvl++;
}
}
if (upgrade.PowType == 3){
if (wep3 == false){
wep1 = false;
wep2 = false;
wep3 = true;
PowLvl = 1;
}
else{
PowLvl++;
}
}
Instantiate(shield_pref, Vector3(transform.position.x,transform.position.y, 2), Quaternion.identity);
shield_pref.particleSystem.Emit == true;
Destroy(col.gameObject);
}
When hit by an enemy ship or projectile, player death: (playerscript agian)
if (col.gameObject.tag == "Enemy" || col.gameObject.tag == "Boss_Shot" || col.gameObject.tag == "asteroid"){
Destroy(col.gameObject);
if (Shield == true){ //if you have a shield, adjust the player texture on hit to remove the shield
renderer.material.mainTextureOffset = Vector2(0.0,0.5);
Instantiate(shield_die, Vector3(transform.position.x,transform.position.y, 2), Quaternion.identity); //play an effect when the shield dies
shield_die.particleSystem.Emit == true;
Shield = false; //boolean disable the shield
return;
}
manager.deathTime = Time.time; //add a brief delay after death
manager.deathTimer = false;
Destroy(gameObject); // destroy the player
Explosion_Pref.particleSystem.Emit == true; //spawn a particle on death
Instantiate(Explosion_Pref, Vector3(transform.position.x,transform.position.y, 2), Quaternion.identity);
}
To respawn the player: (this is the managerscript, located in a "gamemanager" emptyobject so it is always present)
if (player == null && playerLives >= 1 && deathTime == 0){
playerLives --;
Instantiate(playerShip, Vector3(0,-4.5,2), Quaternion.identity);
player = GameObject.FindGameObjectWithTag("Player");
spawn_pref.particleSystem.Emit == true;
Instantiate(spawn_pref, Vector3(0,-4.5, 2), Quaternion.identity);
}
if (player == null && playerLives >= 1 && deathTime > 0 && deathTimer == false){
respawn();
}
and the respawn() function:
function respawn(){
deathTimer = true;
yield WaitForSeconds(1);
playerLives --;
Instantiate(playerShip, Vector3(0,-4.5,2), Quaternion.identity);
player = GameObject.FindGameObjectWithTag("Player");
player.collider.isTrigger = true;
spawn_pref.particleSystem.Emit == true;
Instantiate(spawn_pref, Vector3(0,-4.5, 2), Quaternion.identity);
yield WaitForSeconds(1.5);
player.collider.isTrigger = false;
}
What's with the instantiation of the playership twice? One in the gamemanager and the other in the respawn method.
Also, please post comments as comments, and not answers :(
There is an "add new comment" button under every answer and other comments.
hmm I'm not sure, I'm a noob so I forget to put comments into the code and... I forgot why I did that. Lets see...
ok ok, so one is for spawning the player at the beginning of the game, the other is for spawning the player after death. Doesn't make much sense but .. as I said, I'm still learning/$$anonymous$$ching myself so this was the only way I could figure out how to do this.
I was referring to using the answer mechanism to post your code rather than posting it in a comment :p. Also, don't worry about my code question. I didn't mean to make it sound like I was judging it. I was just wondering if there was any particular reason.
Your answer
Follow this Question
Related Questions
ramdomised weapon pickup 1 Answer
Respawn Time 1 Answer
Random Player Respawn Points 3 Answers
How do I make my player re-spawn in its original place on collision of enemy? 1 Answer
Help - Player Respawn Help 2 Answers