- Home /
Variable having different value in different methods / carrying over value from last play
So basically what's happening is when I start the game, score should be set to 0, and I'm printing it to be sure it is and it prints as 0 on start, but then once a point is scored and it goes up it suddenly picks up from where it left off the last game, if it was 14 last game now its 15 (currently its only increasing by 1 point) at one point I even set it to print score in FixedUpdate and it was constantly printing as 0 but then when i would score the single print would be higher, as if the different methods have different variables for the function
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class gameLogic : MonoBehaviour
{
public float waveSpeed;
public int score;
public playerLaser PlayerLaser;
// Start is called before the first frame update
void Start()
{
waveSpeed = -0.3f;
score = 0;
print(score);
}
// Update is called once per frame
void FixedUpdate()
{
waveSpeed = waveSpeed - 0.00001f;
}
public void enemyScore(int pointsUp)
{
score += pointsUp;
Debug.Log(score);
print(pointsUp);
}
}
How do you start a new game? Does the script reset the scene? If it doesn't then that could cause the issue. If you are not resetting the scene when you start a new game you are not going to call the Start()
method again. You should create a method to set the score to 0 if this is the case.
currently I'm just restarting it with the play button in unity, but i added a print("Game Logic Start Ran"); to the start function and it is running every time, also when I print the score from any method other than enemyScore it prints as 0, but then as higher from the enemyScore method
So if I understood correctly your script doesn't set the score back to 0 when you start a new game?
That prints the variable sent when the enemyScore(int pointsUp) is called from another script, also idk why i called it enemyScore it just triggers when an enemy dies and ups the score, I've since removed that print it was just making sure different enemies sent the right increase amount
Answer by henkehedstrom · Mar 14 at 11:26 AM
Right now, any script could change the score variable on this script because it is public. Do you really need it to be public, if you like that you can see the value and change it in inspector I would recommend that you use SerializeField instead. You could write [SerializeField] private int score. Then we make sure that nothing changes the score from the outside. You often don't want variables to be public like that because it could be harder to keep track on who changes the values and when. If you change it to SerializeField and having it private, the only way score can be changed is in this class or in the inspector. Another class could call on the "enemyScore" function but we could easily track on who does that by breakpointing. With that changed I would like to see how the script that calls on "enemyScore" looks like to be able to help more.
So currently it is set to public to allow it to be accessed by the script which displays the score, ill make it private and add a second variable to do that and see if that works
You could have a get function that returns the value of the variable if you want another script to be able to read the variable or look into properties. I am just saying that by having that variable public it is hard to keep track on when the variable is getting changed.
yeah currently i have it as private and then after its changed a different var which is public is set to it but ill probs change it to that method
So i set it as private and nothing changed, i also set a debug.log to print the score variable in fixedupdate and when it goes up, and so its mostly printing 0 but then when i do get a score up the higher number goes by but then 0s again
Weird, it shouldn't go back to 0 again. Could you send the script that calls on the "enemyScore" function?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerLaser : MonoBehaviour
{
public GameObject explosion;
public gameLogic GameLogic;
private int pointsUp;
private basicEnemy BasicEnemy;
private bool dead;
void start()
{
dead = false;
}
private void Update()
{
}
private void OnCollisionEnter2D(Collision2D collision)
{
//only happens when hits wall as enemies are triggers
Destroy(gameObject);
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("enemy"))
{
//destroies laser
Destroy(gameObject);
//gets which enemy is hit
BasicEnemy = collision.gameObject.GetComponent<basicEnemy>();
//removes one HP then checks if HP = 0 if so returns dead = true
dead = BasicEnemy.Damage();
if(dead)
{
//starts annimation
GameObject effect = Instantiate(explosion, transform.position, Quaternion.identity);
//destroys annimation
Destroy(effect, 1f);
//gets how many points enemy is worth
pointsUp = BasicEnemy.points;
//Destroys enemy
Destroy(collision.gameObject);
//runs enemyScore() with the points enemy is worth
GameLogic.GetComponent<gameLogic>().enemyScore(pointsUp);
}
else
{
//plays animation
GameObject effect = Instantiate(explosion, transform.position, Quaternion.identity);
//destroys animation
Destroy(effect, .1f);
//runs SpriteCheck() to change sprite to match new HP
BasicEnemy.SpriteCheck();
}
}
}
}
thats the script attached to the player laser, it may seem odd im detecting enemy collisions here instead of in the enemy but the enemy has an addition hit box out the front (not tagged enemy) that is used for detecting when other enemies are in front of it so it doesn't fire. so to ensure it only triggers when hitting the enemy not its detector i put it in laser. also it needs to check the points because every enemy has a chance to spawn with a higher hp and higher score value so it can't be a set increase number
But yeah its basically acting as if the score variable in public void enemyScore(int pointsUp)
is diffrent from the one being used in start and fixedupdate, with it printing as 0 from those even after the score goes up but not resetting for the enemyScore at start
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to get a variable out of a coroutine? 3 Answers
Need to use 2 different language scripts. 1 Answer
Basic C# Public Variable Help 3 Answers