Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Tekksin · Dec 19, 2013 at 06:35 AM · guiplayerplayerprefsrespawndeath

GUI not counting Player Deaths

There is something going on with my player or enemy script code. After I die, many functions are not useable. The first I noticed was a powerup that works when the player comes in contact with an enemy. It will work until the player dies. Then it and other functions stop working.

Now that I'm working on my GUI, I'm noticing that it isn't counting the number of times the player is dying. (side note, not sure if I want to count the number of times the player dies, or count it down from a set amount until getting a game over.) Note: it counts the first death, but then nothing afterwards, no matter how many different ways I try to kill the player.

I have a counter used for counting coins, and regardless of whether or not the player dies, the tally for that works as expected because it has nothing to do with my enemy and playerscript. Clearly something is wrong with how the player dies, right?

My skype is: Tekksin

The part of my enemy script involving this issue reads:

 private var done = false;
 
 function OnTriggerEnter(other : Collider){
 if(!stomp){    
         if(other.tag == "Player"){
             if(player.Diamond == true){
                     coin = Instantiate(thePrefab, transform.position, Quaternion.identity);
                     Destroy(gameObject);
                 }
                 else if(player.Pick == true && player.Diamond == false){
                     player.Pick = false;
                     return;
                 }
                 else {
                     var Player : GameObject = Instantiate(Player, spawnPoint.position, Quaternion.identity);
                     Destroy(other.gameObject);
                     if(!done){
                         Lives.lifeTotal =- 1;
                         done = true;
                     }
             }
         }
     }
 }


There isn't a relevant part of my "player script" that I see could interfere with this. The respawning function isn't on it, so here is my respawn script:

 private var done = false;
 var Player : GameObject;
 var spawnPoint : Transform;
 
 function OnTriggerEnter(other : Collider) {
     if(other.tag == "Player"){
         Destroy(other.gameObject);
         var Player : GameObject = Instantiate(Player, spawnPoint.position, Quaternion.identity);
         if(!done){
             Lives.lifeTotal =- 1;
             done = true;
         }    
     }
 }

here is my GUI script:

 static var lifeTotal = 0;
 
 function OnGUI (){
     guiText.text = "Lives: " + lifeTotal;
 }


I hope I'm not missing some info. What the hell am I doing wrong??

Comment
Add comment · 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 Tekksin · Dec 19, 2013 at 10:13 AM 0
Share

Example of my problem:

  1. I start the game, run into an enemy: the enemy kills me and my death count reads "-1" on the GUI (because I start with 0 lives at the moment lol). The player then instantiates.

  2. I start the game, run into a bottomless pit: I die, my death counter reads "-1" and the player instantiates.

  3. I start the game, run into an enemy: the enemy kills me and my death count reads "-1". The player then instantiates. I then use the instantiated character to run into a bottomless pit: the GUI still reads "-1". I can then continue to kill the character in any other way and the death count remains unchanged since the initial death.

So it's not that the enemy is disabling the GUI from continuing it's count, and the same for the death by bottomless pit.. Something about respawning breaks the script so that the tally for deaths cannot be accrued. I can mix and match the order and way in which I day, and the tally will still only count "1" of the deaths.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Tomer-Barkan · Dec 19, 2013 at 07:43 AM

One problem I can see, but there could be more since you shared so little of your code, you didn't even share the declaration of variables (what is Player for example?):

 var Player : GameObject = Instantiate(Player, spawnPoint.position, Quaternion.identity);

The temporary variable that you define uses the same name as the prefab. Try:

 var newPlayer : GameObject = Instantiate(Player, spawnPoint.position, Quaternion.identity);

Where Player is a prefab, and you do not make any modifications to it. If you want to store a reference for the instance of the player, store it in a separate variable than the prefab, because you will need the prefab reference to instantiate new players.

Comment
Add comment · 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 Tekksin · Dec 19, 2013 at 10:00 AM 0
Share

Thanks so much for the reply.

Player refers to the character being controlled. I don't do code with commenting because it makes me feel like I'm cluttering things up. I changed the name, but I'm unsure why you asked me to..

edit:

I changed the variable to read:

var Player : GameObject = Instantiate(newPlayer, spawnPoint.position, Quaternion.identity);

and the same problem occurs. I subsequently edited the variable at the top of that (enemy) script:

var newPlayer : GameObject;

to get the new codeword "newPlayer" to work in that variable statement.

avatar image Tomer-Barkan · Dec 19, 2013 at 11:47 AM 0
Share

Hi,

You have a few options. If you want to respawn the player by recreating an object, you need the player to be a prefab, and then when he dies you instantiate a new copy of the prefab, and destroy the old copy of the prefab. Learn about prefabs in the unity tutorials if you are unfamiliar.

Another option is to not destroy and instantiate the player object, but ins$$anonymous$$d manually move it back to the starting position, and any other variables that need to be reset after the player dies.

avatar image Tekksin · Dec 19, 2013 at 07:53 PM 0
Share

that is exactly what is happening. The player is being instantiated new and the old one is destroyed. What from my code tells you different?

And the second option you propose is interesting and is something I'm currently looking into. I just don't know how to translate the character. Any ideas? like I said, I'm researching it now. If I find out, I'll post my new code and the effects of it.

avatar image Tekksin · Dec 19, 2013 at 08:29 PM 0
Share

it's because my script reads "lives.lifeTotal =-1;" which essentially reads "lives = -1" =_= lol. It was updating, it was just restating that the lives value was -1 over and over again lol.

SUCH A STUPID $$anonymous$$ISTA$$anonymous$$E I can't believe nobody said anything about it lol. it's supposed to be "-=" not "=-" lol. damn. All the waste hours. Oh well.

avatar image
0

Answer by KellyThomas · Dec 19, 2013 at 12:10 PM

Your respawn script says:

 private var done = false;
 
 //...
 
 if(!done){
     Lives.lifeTotal =- 1;
     done = true;
 } 
 

  • Firstly "`Lives.lifeTotal =- 1`" will assign -1 to lifeTotal, do you mean "`Lives.lifeTotal -= 1`" which will decrement lifeTotal?

  • Secondly are you resetting done to false elsewhere? If not this code will only execute once.

Edit: I just noticed the exact same code is in your enemy script. Both of these "`done`"s are private instance variables i.e. they do not share a value. I suggest you consider why they are there and find approach for the problem they are intended to solve.

Comment
Add comment · 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 Tekksin · Dec 19, 2013 at 07:49 PM 0
Share

I'm totally aware of what you first mentioned. I said in the description I'm unsure of how I want the lives to work, and inconsequential things like that will be addressed when the main problem is sorted.

Anyways, under "done = true;" I put "done = false;" which effectively makes it all false again, right? I did this in both scripts and there is no difference in the GUI. There aren't any erros and the game also plays no different.

I'm also unsure what the problem is with private variables?

avatar image
0

Answer by Tekksin · Dec 19, 2013 at 08:33 PM

ok, and now this site isn't letting me reply anymore, or mark KellyThomas' response as correct. Only after I figured out my problem did I notice it's exactly what he said (totally went over my head). I found out when I tried just putting "++" instead of "=-" in that lifeTotal spot.

Anyways, now the forum has problems, for me lol. so random.

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

20 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 avatar image avatar image avatar image

Related Questions

Help with calling and naming functions! 2 Answers

playerprefs 0 Answers

Player Respawn Script - Not Working 1 Answer

PlayerPrefs Saving Player's Position Java Script 2 Answers

Cant Find Destruction Code? 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