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 :-|
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. :)
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?
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...
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....
Your answer
Follow this Question
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