Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 litafbrah · Nov 26, 2018 at 09:39 PM · 2d gamescene-switchingdontdestroyonloadscore systemscene-change

When die - reset score from the last scene

Hello there,

I have this simple score script for 2D. When you are in the Scene number1 (where main canvas is attached to) and you collect collectibles - the score increases and whenever you die - it resets to zero again. If I manage to complete the level and get (load) to another scene, the score it still there from the previous scene which is fine. Although, in "that next scene", if I pick additional collectible(score), which adds up to my previous scene score, and die, I still keep my new "fake" score. Basically, I can get infinite score.

What I want to do is, as example, if I had a score of 5 from Scene1 and I load a Scene2 with the same score of 5, so in case I collect additional 5 in Scene2 and die, get respawned at the beginning, I want my score to become the one from the Scene1 which is 5 because right now it becomes 10.

Code is really simple:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Score : MonoBehaviour {
     public static int score;
     Text scoring;
     public GameObject mainCanvas;
 
     private void Awake()
     {
         DontDestroyOnLoad(mainCanvas);
     }
     void Start()
     {
         scoring = GetComponent<Text>();
         score = 0;
     }
     void Update () {
         scoring.text = "Score: " + score.ToString();
     }
 }

And I have in my collectible script.

 <...> 
 score++; 
 <...>
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by GreenSerpent · Nov 26, 2018 at 10:04 PM

When you die, on whatever script you use to kill the player, access the score script and set the score to 0.

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 litafbrah · Nov 26, 2018 at 11:11 PM 0
Share

It resets back to zero completely. I want to keep the last score from the previous scene even when I die in my next scene. I just want to lose the points that I receiver during my new scene. Here is the code by the way for player. Die (killbox tag) is at the very end:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Scene$$anonymous$$anagement;
 
 public class bird2 : $$anonymous$$onoBehaviour {
 
     private Animator ani;
     // 
     public bool rip = false;
 
     // $$anonymous$$ovement speed
     public float speed = 2;
 
     // Flap force
     public float force = 2000;
 
     private Rigidbody2D body;
     AudioSource audioWings;
     public AudioClip DeathAudio;
 
     // Declaration for Death Counter
     public float DeathCounter;
     
     // Use this for initialization
     void Start()
     {
         body = GetComponent<Rigidbody2D>();
         GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
         ani = GetComponent<Animator>();
         audioWings = GetComponent<AudioSource>();
     }
 
     // Update is called once per frame
     void Update()
     {
         // This checks if the bird is dead or not, if it is then the bird cannot be controlled.
         if (rip == false)
         {
             // Flap Control
             if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A))
             {
                 ani.SetTrigger("up");
 
                 body.AddForce(Vector2.up * force);
 
                 //Play Flap Sound
                 audioWings.Play();
             }
         }
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "winterobs") // Detects if bird has entered collidor with tag "winterobs"
         {
             ani.SetTrigger("dead");
             rip = true; //Stops Bird Control
             body.velocity = Vector2.zero; //Stops Bird $$anonymous$$ovement
             GetComponent<AudioSource>().PlayOneShot(DeathAudio);
         }
         if (other.tag == "killbox") // Detects for tag on collider again
         {
             DeathCount.DeathValue += 1; ;
             Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().name); //Reloads the active scene
         }
     }
 
  
 }
 

avatar image
0

Answer by riskaanisah · Dec 09, 2018 at 12:24 AM

hey @litafbrah may I ask I have a problem similar to yours

I have a game with 10 questions, where every 1 scene is a question. I want to display the score in each scene, can you help me

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

111 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 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 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 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 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

DontDestroyOnLoad duplicate prevention code is deleting the original player 2 Answers

(2D) Can't make a scene transition between 2 game objects colliding - 2019 1 Answer

How do you transfer UI Text from one scene to another? 1 Answer

Best way to deal with DontDestroyOnLoad when returning back to the same scene? 1 Answer

keep static/global object through scenes 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