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;
}