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 Conect11 · Nov 07, 2013 at 03:34 AM · playerprefshealth

[SOLVED] Same script (virtually) on two gameObjects. One saves to playerprefs, one doesn't

Ugh. Sorry I'm asking this. I have two gameobjects, boss characters. Each has (nearly) the same script attached, a health script. The one works perfectly, and saves the result of the battle to playerprefs, so that after you've defeated him, he's gone for good.

But the other one...

The results of the battle never save. He just reappears every time the game is started, and I don't get it. Clearly, I've done something wrong, can't see something, have bad syntax somewhere, I don't know. Am hoping fresh eyes might see what I'm missing, because I'm banging my head against the keyboard after spending hours on this. The two scripts are below, starting with the working one. God bless, and thanks. Sorry to ask such a stupid question.

Working Script

 var Name = "Guardian";
 var Exp = 150;
 static var GuardianHealth : int = 75;
 var GuardianKilled = 0;
  
 function Start(){
  if(PlayerPrefs.GetInt("GuardianDead", 0) == 1){
   //object.active = true;
   //object.enabled = true;
 TowerGateOpen.GateOpen = true;
 Destroy(gameObject);
 }
 }
 
      
 
     function OnTriggerEnter (other : Collider){
     if (other.gameObject.name == "sword") {
     GuardianHealth = -10;
     }
     else 
     {
     if (other.gameObject.name == "club")
     {
         GuardianHealth = -1;
     
 }
 
 
  
 if(GuardianHealth <= 0 )
 if(GuardianKilled == 0 )
 {
 GuardianHealth = 0;
 GuardianKilled = 1;
 audio.Play();
 Playerhealth.curXp += Exp;
 Playermoney.curMoney += 350;
 TowerGateOpen.GateOpen = true;
 PlayerPrefs.SetInt("GuardianDead", 1);
 Debug.Log( Name + " is killed");
 Destroy(gameObject);
 }
 }
 }


Script that won't save to playerprefs:

 var Name = "Medamomo";
 var Exp = 450;
 static var MedamomoHealth : int = 5;
 var MedamomoKilled = 0;
 
  
 function Start(){
 if(PlayerPrefs.GetInt("MedadmomoKilled", 0) == 1){
   //object.active = true;
   //object.enabled = true;
 MedamomoGateOpen.GateOpen = true;
 Destroy(gameObject);
 }
 }
 
      
 
     function OnTriggerEnter (other : Collider){
     if (other.gameObject.name == "sword") {
     MedamomoHealth = -7;
     }
     else 
     {
     if (other.gameObject.name == "club")
     {
         MedamomoHealth = -1;
     
 }
 
 
  
 if(MedamomoHealth <= 0 )
 if(MedamomoKilled == 0 )
 {
 MedamomoHealth = 0;
 MedamomoKilled = 1;
 //audio.Play();
 Playerhealth.curXp += Exp;
 Playermoney.curMoney += 1550;
 PlayerPrefs.SetInt("MedamomoKilled", 1);
 Debug.Log( Name + " is killed");
 MedamomoGateOpen.GateOpen = true;
 Destroy(gameObject);
 }
 }
 }
Comment
Add comment · Show 2
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 Huacanacha · Nov 07, 2013 at 04:08 AM 0
Share

Can you see if you can get the indenting working properly? I'm struggling to make much sense of it with the current formatting!

Is there a reason the Health variables are static and the $$anonymous$$illed ones aren't?

For sword strikes do you mean to do health -= hitAmount? Currenty sword strikes and club strikes are instant kills as you set the health to 0 or -something.

Have you checked that the $$anonymous$$edamomo$$anonymous$$illed variable doesn't have a different value set in the inspector? This could cause it to be something different to 0 to start with, meaning that the killed logic (inc. setting PlayerPrefs) won't be execited

avatar image Conect11 · Nov 07, 2013 at 04:12 AM 0
Share

hey, still working on my formatting, but it was a really simple fix. I misspelled something, which I kinda suspected.

As of right now, both sword and club ARE one hit kills. That will change for the player, just set his hp low for testing purposes.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Josh707 · Nov 07, 2013 at 04:01 AM

Between lines 5 & 10, you seem to have misspelled "MedamomoKilled" as "MedadmomoKilled", since player prefs don't throw errors with unset values that might be the issue.

Comment
Add comment · Show 2 · 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 Conect11 · Nov 07, 2013 at 04:05 AM 0
Share

that was it man. You are totally awesome, thank you!!!!!

avatar image Josh707 · Nov 07, 2013 at 04:10 AM 0
Share

Haha no problem!

avatar image
0

Answer by Owen-Reynolds · Nov 07, 2013 at 04:08 AM

Just glancing, GuardianHealth is a static, which is reset to 0(?). This means when the 1st guy is killed, the 2nd guy has 0 health. Static is really special purpose. A "normal" variable would give them each their own health. Then sword and club hits have GuardianHealth = -10;, meaning they insta-kill the guy. You probably meant to use -= to subtract 10 health.

Then the death check (the two ifs) specifically says to ignore death if you're already killed a guy.

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

18 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

Related Questions

Playerprefs not work!? 0 Answers

Health bar depletes when app isn't open 1 Answer

update highscore if current is higher than previous score 0 Answers

this is part of my health script it gives me a error unknown identifier GameScore pls help thxxx 0 Answers

Health script, save health in between scenes. PlayerPrefs. 2 Answers


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