• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by asylum101 · Nov 14, 2013 at 09:35 PM · playerrespawnpickuppowerup

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 s$$anonymous$$p, 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.

Comment

People who like this

0 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GameVortex · Nov 14, 2013 at 09:43 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by ThePunisher · Nov 20, 2013 at 10:16 PM

Wait, I t$$anonymous$$nk 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(s$$anonymous$$eld_pref, Vector3(transform.position.x,transform.position.y, 2), Quaternion.identity);
         s$$anonymous$$eld_pref.particleSystem.Emit == true;
         Destroy(col.gameObject);
     }

But you never get another reference to the upgrade. Can place t$$anonymous$$s code real quick right after the "wep_up" condition passes.

 upgrade = col.gameObject.GetComponent(upgrade_script);
Comment
asylum101

People who like this

1 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ThePunisher · Nov 21, 2013 at 06:51 PM 1
Share

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?

avatar image

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 everyt$$anonymous$$ng into notepad because t$$anonymous$$s 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.S$$anonymous$$psKilled++; 
     manager.points = manager.points + 100; 
     number = Random.Range(minRNG, maxRNG);
     if (number == 1) {
         number = Random.Range(minRNG, maxRNG);
         if (number > 5){ //if roll is $$anonymous$$gher than 5, spawn s$$anonymous$$eld, if less than, spawn wep powerup, min/max default is 1/10
             Instantiate(s$$anonymous$$eld_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 s$$anonymous$$p
   if(player.wep2 == false){
      Destroy(gameObject); // destroy the weapon particle if it's not wep2.
   }
 }


aaaand to apply/delete the power up: (t$$anonymous$$s 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(s$$anonymous$$eld_pref, Vector3(transform.position.x,transform.position.y, 2), Quaternion.identity);
             s$$anonymous$$eld_pref.particleSystem.Emit == true;
             Destroy(col.gameObject);
         }
 

When $$anonymous$$t by an enemy s$$anonymous$$p or projectile, player death: (playerscript agian)

  if (col.gameObject.tag == "Enemy" || col.gameObject.tag == "Boss_Shot" || col.gameObject.tag == "asteroid"){
                 Destroy(col.gameObject);
                 if (S$$anonymous$$eld == true){ //if you have a s$$anonymous$$eld, adjust the player texture on $$anonymous$$t to remove the s$$anonymous$$eld
                     renderer.material.mainTextureOffset = Vector2(0.0,0.5);
                     Instantiate(s$$anonymous$$eld_die, Vector3(transform.position.x,transform.position.y, 2), Quaternion.identity); //play an effect when the s$$anonymous$$eld dies
                     s$$anonymous$$eld_die.particleSystem.Emit == true;
                     S$$anonymous$$eld = false; //boolean disable the s$$anonymous$$eld
                     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: (t$$anonymous$$s is the managerscript, located in a "gamemanager" emptyobject so it is always present)

 if (player == null && playerLives >= 1 && deathTime == 0){
         playerLives --;
         Instantiate(playerS$$anonymous$$p, 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(playerS$$anonymous$$p, 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;
             
 }
 
Comment

People who like this

0 Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ThePunisher · Nov 14, 2013 at 11:56 PM 0
Share

What's with the instantiation of the playership twice? One in the gamemanager and the other in the respawn method.

avatar image ThePunisher · Nov 15, 2013 at 12:04 AM 0
Share

Also, please post comments as comments, and not answers :(

There is an "add new comment" button under every answer and other comments.

avatar image asylum101 · Nov 15, 2013 at 04:56 PM 0
Share

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/teaching myself so this was the only way I could figure out how to do this.

avatar image ThePunisher · Nov 19, 2013 at 06:58 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Respawn Player after Health 0 1 Answer

Making a powerup that transforms player scale 1 Answer

Player lives script 1 Answer

What to do with Player game object while he is waiting to respawn. 1 Answer

Falling off respawn script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges