Int does not reset on start, even though I tell it to...

Hello helpful people of this place!

I have a score script that I’m trying to setup, and I want the score int to reset to 0 every time I start the game. I’ve done this by putting this code on the start of my player script:

public int score;

void Start () {
		score = 0;	
	}

Now I have a script on my enemies that tell them to += the score when destroyed, like this:

void OnTriggerEnter (Collider other) {
	
		if (other.tag == "Bullet") {
			
			playerobj.GetComponent<ShipControls>().score += 1;
			
			Destroy (this.gameObject);
			
			
		
	}

…and this kinda works. When I shoot the enemies, they get destroyed and they update my player prefab score int, but not immediately the player instance int that is in scene. Also, when I stop the game and start it up again, the score is saved in the prefab from the last time I played, racking the score up instead of reseting as I would’ve hoped for with the “score = 0;” on my void start function. The player instance in the scene gets it’s score int updated first when I stop the game, as if it’s getting updated by the prefab first after I’ve stopped playing.

What am I doing wrong here? Lemme know if I can provide any other info to make my issue clearer… Thank you in advance!

What do you mean by ‘stopping the game and starting it up again’? Do you mean you actually hit the stop button within the Unity Editor to terminate the game, and then hit the play button to start it again?

Normally, you can’t change a prefab from code. The prefab will still contain the same values and info after terminating the game.

If you mean that you’re starting/stopping the game using a feature you wrote in your own game, you may want to test if the Start() method of your player object is called - if you’re not destroying and re-creating your player object, the Start() method won’t be called again (it’s only called once, after the object has been instantiated).

Thanks for the reply! I mean hitting the stop button within the Unity Editor. And yeah, weirdly enough, it changes the int that’s in the prefab. Whenever I terminate and restart, the public score int on the prefab stays permanently changed.

Here’s my player control script:

public class ShipControls : MonoBehaviour {
	
	public Transform bullet;
	public Transform startPos;
	public Transform explode;
	public Transform enemyBullet;
	public Transform boss;
	public Transform spawnPoint;
	
	public int moveSpeed = 5;
	public int lives;
	public int score;
	
	public GUIText lifeCount;
	
	public bool canMoveLeft = true;
	public bool canMoveRight = true;
	public bool canMoveUp = true;
	public bool canMoveDown = true;
	
	void Start () {
		Instantiate (startPos, transform.position, transform.rotation);
		score = 0;
		
		
	}
	
	void Update () {
		
		InputControl();
		
		//Vector3 bossSpawn = spawnPoint.position;
		
		//if (score == 21) {
		//	Instantiate (boss, bossSpawn, transform.rotation);
			
		//}
		
		
	}
	
	void InputControl() {
		if (Input.GetKeyDown(KeyCode.Space)) {
			Instantiate (bullet, transform.position, transform.rotation);
			//Shoot();
			
			
			
		}
		
		Vector3 moveVec = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
		if ((moveVec.x > 0  !canMoveRight) || (moveVec.x < 0  !canMoveLeft)) {
			moveVec.x = 0;
		}
		if ((moveVec.z > 0  !canMoveUp) || (moveVec.z < 0  !canMoveDown)) {
			moveVec.z = 0;
		}
		transform.Translate(moveSpeed * moveVec * Time.deltaTime);
	}
	
	void OnTriggerEnter (Collider other) {
		if (other.tag == "enemyBullet") {
			
			if (lives < 1) {
				Destroy (this.gameObject);
			} else {
				lives -= 1;
				Instantiate (explode, transform.position, transform.rotation);
			transform.position = startPos.position;
			}
			
			
			
		}
	}
}

And here is my enemy script:

public class InvaderScript : MonoBehaviour {
	
	public Transform Bullet;
	public Transform shipExplotion;
	public Transform myBullet;
	
	public GameObject playerobj;
	
	private float Counter;
	public float moveTimer;
	
	public bool movingRight;
	public bool movingLeft;
	
	// Use this for initialization
	void Start () {
	Counter = Random.Range (0f, 5.0f);
		
	moveTimer = 10;
	}
	
	// Update is called once per frame
	void Update () {
		
		moveTimer -= Time.deltaTime;
	
		if (moveTimer > 5) {
			movingRight = true;
		} else {
			movingRight = false;
		}
		
		
		if (moveTimer < 5) {
			movingLeft = true;
		} else {
			movingLeft = false;
		}
		
		if (movingRight == true) {
		transform.Translate (-0.1f, 0, 0);
		}
		
		if (movingRight == false) {
		transform.Translate (0, 0, 0);
		}
		
		if (movingLeft == true) {
			transform.Translate (0.1f, 0, 0);
		}
		
		if (movingLeft == false) {
			transform.Translate (0, 0, 0);
		}
		
		
		if (Counter < 0) {
			Counter = Random.Range (1.0f, 10.0f);
		} 	else {
			Counter -= Time.deltaTime;
		}
		//print (Counter);
		if (moveTimer < 0) {
			moveTimer = 10;
		}
		
		if (Counter < 0f) {
		Instantiate (myBullet, transform.position, transform.rotation);
		}
	
	}
	
	void OnTriggerEnter (Collider other) {
	
		if (other.tag == "Bullet") {
			
			playerobj.GetComponent<ShipControls>().score += 1;
			
			print (playerobj.GetComponent<ShipControls>().score);
			
			
			Instantiate (shipExplotion, transform.position, transform.rotation);
			
			Destroy (this.gameObject);
			
			
		
		}
	}
}

Now, I’m telling the enemy to update the ShipControls script before destroying itself, but as said, it changes the prefab permanently and not the scene instance until after I’ve terminated the game. Super frustrating as I feel like I’ve set it up properly… Also, despite my Score = 0 on void start, the score just stays the same from when I terminated last.

What is the ‘playerobj’ parameter of the InvaderScript behavior pointing to - the instance of the player in the level, or the prefab in the project? If it’s pointing to the prefab, that would explain this behavior - things in the project rather than the level (prefabs, materials, …) do keep changes made during gameplay.