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 mattsmith8383 · Feb 16, 2014 at 04:08 AM · c#scenevariablestaticreset

Death Counter Resets When Reloading Scene.

I've tried a number of things here, but I can't seem to get the variables to stop resetting. I tried changing the integer to a static int, but it still reset. I also read that static variables should be avoided in most instances.

The script is attached to the player, and the death counter code I have is as follows:

     void OnTriggerEnter(Collider other) {

     if (other.gameObject.tag == "Dead") 
            {
         Application.LoadLevel (Application.loadedLevel);
         DeathCount++;
         SetDeathText ();
         }
    }


If it helps in narrowing down the issue, as a test I also set the death counter to increase after a key is pushed, and the level to reset after a different one is pushed. The variables reset regardless.

Thank you.

Comment
Add comment · Show 5
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 Benproductions1 · Feb 16, 2014 at 06:19 AM 0
Share

Please post all relevant code. In this case, anything doing anything to DeathCount should be posted. As it stands this question remains unanswerable.

avatar image mattsmith8383 · Feb 16, 2014 at 07:22 AM 0
Share

That's pretty much it. Touch object, increase death count, reload.

I left this part out mistakenly:

     void SetDeathText()
     {
     deathText.text = "Deaths: " + DeathCount.ToString();
     }
avatar image Benproductions1 mattsmith8383 · Feb 16, 2014 at 08:02 AM 0
Share

Firstly: Don't post comments as answers.
Secondly: So you never declare your variable?

avatar image Benproductions1 · Feb 16, 2014 at 12:53 PM 0
Share

The only question you answer in the answers section is THE question, not just any question a passer by asked. You're probably setting your variable from somewhere else if the static keyword didn't solve your problem, that's why I asked you to post all relevant code, including the variable declaration.

Now would you kindly drop the attitude so I won't have to forcibly remove your comments.

avatar image mattsmith8383 · Feb 16, 2014 at 07:34 PM 0
Share

Firstly: Get off your high horse and quit being a diva. $$anonymous$$y comment was harmless, I just pointed out that you didn't read my post when you responded to me so condescendingly and pompously.

Secondly: You're not here to help. You're here for an ego-stroke and on a power trip because you're such a big-shot who can forcibly delete posts.

Thirdly: I've got my answer, so go ahead and delete this comment too so everyone knows you're a sensitive little diva that can't handle a bit of sarcasm.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by MadJohny · Feb 16, 2014 at 12:57 PM

Put this on the deathcount script on function awake:

 DontDestroyOnLoad(transform.gameObject);

http://docs.unity3d.com/Documentation/ScriptReference/Object.DontDestroyOnLoad.html

or you can set the deathCount variable as a playerPref: https://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html so when you die:

PlayerPrefs.SetInt ("deathCount", PlayerPrefs.GetInt("deathCount") + 1); (untested)

And so, to display the score you would go with a PlayerPrefs.GetInt("deathCount");

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 Benproductions1 · Feb 16, 2014 at 01:13 PM 0
Share

I believe that and player prefs calls perform IO operations and should therefore be avoided. Cache the variables ins$$anonymous$$d and only save when the object is destroyed.

avatar image MadJohny · Feb 16, 2014 at 02:11 PM 0
Share

The reason why I referred deathCount is because of a game I played (rage game made by EteeskiTutorials and JesseEtzler0 for the Ludum Dare competition) and it had a death counter which was saved, so everytime you came back to play the death count was still the same

avatar image mattsmith8383 · Feb 17, 2014 at 09:47 AM 0
Share

$$anonymous$$adJohny - This is the kind of response I was looking for. Thanks.

With the DontDestroyOnLoad command, I assume I need additional code to prevent multiple instances from spawning upon reloading the existing scene? Having the code attached to the player caused rather unfavorable results. I attempted setting up an empty gameobject with the code, but the results were still a little glitchy with the empty gameobject also spawning multiple instances.

avatar image MadJohny · Feb 17, 2014 at 09:56 PM 0
Share

So ins$$anonymous$$d of putting the same gameOjbect on the gameplay scene, try putting it in an empty scene, (and not on the gameplay scene), the empty scene shall instaload to the gameplay seen, so you have that one instance, and you don't have more than one, if it works accept my answer pls

avatar image
0

Answer by Bennyboy50z · Feb 16, 2014 at 07:33 AM

Hi There! I've also heard a bit that static variables should be avoided but if you do decide to use it for your death counter I just tested your script with a static variable in JavaScript and it worked! Just a few small changes and you should be rolling! Goodluck.

 static var DeathCount: int = 0;
 var targetGuiText : GUIText;
     
     function OnTriggerEnter(other: Collider) {
     if (other.gameObject.tag == "Obstacle1"){
     Application.LoadLevel ("Level1");
     DeathCount++;
     SetDeathText ();
     }
     }
     
     function SetDeathText(){
     targetGuiText.text = "Deaths:" + DeathCount;
     }
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 mattsmith8383 · Feb 16, 2014 at 08:18 AM 0
Share

Thanks for trying it out. I did some tests with static variables and I can manage to make them stay after resetting the scene, but the death counter still isn't staying. I'll keep at it and respond back if I make any progress. Thanks again.

EDIT:

Literally just copied and pasted my own code below the existing code with different variable names and for some reason it works. The death counter doesn't work still, but the new one functions exactly the same.

Seems I can't up vote you yet, but thanks again for the reassurance. I'm still scratching my head at this, but at least it's working now.

avatar image Bennyboy50z · Feb 16, 2014 at 09:22 AM 0
Share

Haha that's alright, as you said at least it's working. Glad to have helped.

avatar image
-2

Answer by Luka-L · Mar 02, 2017 at 02:36 PM

I did find an answer if you still need it :D

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 Juwairia_ · Apr 27, 2020 at 09:45 PM 0
Share

yes, I need it.

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

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

Related Questions

Where is the Mistake in my Coin System? 1 Answer

I´ve got a problem with my Coin System. It always shows 1 Coin. 2 Answers

Help with comparing what scene is loaded 1 Answer

Static variable error 2 Answers

How to access variables from other scripts without static (javascript) 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