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 /
  • Help Room /
avatar image
0
Question by Sirevix · May 06, 2016 at 02:20 PM · errorbugsceneloadingscore

when i load scene 2, score adds automatically x6 (Weird bug or something)

so.. i have this game where you get 1 point for killing an enemy if the enemy touches you it loads another scene (scene 2)

but if i kill the enemy i score the point then another enemy kills me it loads the scene 2 and the score reads 7... if my score is 2, on the secound scene will be 12

why this happens?

i want just to keep my score as it was in scene 1

here is my code

 using UnityEngine;
 using System.Collections;
 
 public class Score : MonoBehaviour {
 
 
     public static int count = 0;
 
     // Use this for initialization
     void Start () {
 
         Cursor.lockState = CursorLockMode.Locked;
 
     }
 
 
     // Update is called once per frame
     void Update () {
 
         if (count == 200) {
 
             print ("lol");
 
             Instantiate(Resources.Load ("Cube (1)")); count ++;
         }
 
         if (count == 300) {
 
             print ("lol");
 
             Instantiate(Resources.Load ("Cube (1)")); count ++;
         }
     }
 
     void OnGUI () {
         GUI.Label (new Rect (48, 10, 100, 30),"Kills = " + count);    
     }
 
 }

and another script in case you need it

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement;
 
 public class Player_Health : MonoBehaviour {
 
 
 
     public AudioClip heart;
     public int health = 20;
     public int TheDammage = 5;
     public int TheCure = 50;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
 
 
     }
 
     void Awake ()
     {
         DontDestroyOnLoad (this.gameObject);
     }
 
 
 
     void OnGUI () {
 
 
 
         GUI.Label (new Rect (50, 25, 100, 30),"health = " + health);
 
     }
 
 
 
     void OnCollisionEnter (Collision collision) {
 
 
         if (collision.gameObject.tag == "Healer") {
             health += TheCure;
         }
 
 
         if (collision.gameObject.tag == "Enemy") {
 
             health -= TheDammage;
 
         }
 
         if (health <= 0) {
             KillPlayer ();
 
         }
 
 
 
 
         if (health <= 150) {
 
             GetComponent<AudioSource>().Play ();
         }
 
         if (health >= 200) {
 
             GetComponent<AudioSource>().Stop ();
         }
 
 
     }
     public void KillPlayer(){
 
 
         SceneManager.LoadScene ("2" );
         health += 200;
 
         //Destroy (gameObject);
         //transform.gameObject.tag = "Finish";
     }
 }
 



and... im not done yet xD

for the score to work i have this on my enemy

using UnityEngine; using System.Collections;

public class EnemyHealth : MonoBehaviour {

 public int scoreValue = 1;
 public int enemyHealth = 20;
 public int TheDammage = 5;

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 


 }

 void OnCollisionEnter (Collision collision) {

     if (collision.gameObject.tag == "bullet") {

             enemyHealth -= TheDammage;

         }

         if (enemyHealth <= 0) {
             Destroy (gameObject);
         }
     }

 void OnDestroy(){

     Score.count += scoreValue;    
 }
 }


i've readed these scripts over and over again and i can't figure nothing

what's wrong? im verry bad at scripting but this is soo weird that i can't figure it out by my own :-|

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 jtok4j · May 07, 2016 at 06:59 AM

Usually, in my limited experience, I found that some scripts in my projects were being run multiple times between scene loads, due to the scripts loading/unloading, and loading others linked/referenced in them.

I went through your scripts, but it's 12:56am here, and I'm not seeing anything that strikes me as the cause.

Hope you find it soon. :)

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 tanoshimi · May 07, 2016 at 07:21 AM 0
Share

I agree with the diagnosis, and I think the culprit is Player_Health, which is marked as DontDestroyOnLoad (this.gameObject);. But is this present in the hierarchy of Scene 2 as well? If it is, then you'll get two copies of it. It should be pretty easy to verify if this is the case by just looking though the scene hierarchy in the editor at the point the problem occurs - do you have multiple instances of this script attached to your object?

avatar image Sirevix tanoshimi · May 07, 2016 at 01:46 PM 0
Share

the hierarchy on scene 2 have one cube as a terrain and a dirrectional light i thought of that and erased everything :-| it doesn't double the score it adds 7 points i've created a menu for my game yesterday and when i click play it loads the scene 1 "the game" and it automatically give's me 4 or 6 points just for loading the game scene :-| this is soooo weird...

avatar image
0

Answer by Sirevix · May 09, 2016 at 11:07 PM

the problem was solved here...

http://forum.unity3d.com/threads/loading-scene-changes-static-int-values-by-itself.403247/

the problem was that... when scene unloads to load a new one it destroy's every enemy in my hierarchy... i couldn't figure out how to solve it, but i know what caused the problem....

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

68 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

Related Questions

Default plane object display error. 0 Answers

Iterator error upon trying to load scene 0 Answers

Unity crashes every time when trying to open project 0 Answers

Can't load one unity project (which I'm working on) but can load all others! 0 Answers

Hello everyone I have a problem with rounding number of my score 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