Score system

hello folks im working on my score system and I seem to have it down but the problem is every-time a projectile is shot the score goes up but when the “Clone” of the projectile is destroyed the point goes away so my score resets to zero every-time I’m assuming it has to do with the projectile clone instantiating then destroying does anyone have a clue how to fix this? im using prefab version of shooting would ray-casting be better? thanks in advance guys

ah yes, the prefab shooting solution…

care to share your code so we have a hope of finding where you’ve gone off piste?

You need to have an object in the scene with a script attached that keeps track of the score. Before the projectile is destroyed you want to find the object in the scene with your score script and add to that.

lol yes i see its common lol ill upload in a bit

well its attached to the projectile which i think is maybe the problem while using the prefab way , i had it attached to the player which is kinda what i wanted seeing as the score was based on being hit but i couldnt quite figure out how to add to the score if the projectile missed the player so i moved it to the projectile itself and it workings aside from the resetting of the score after each instantiation.

Here is my simple scoring:

void OnCollisionEnter(Collision Hit)
	{
		
		if(Hit.gameObject.name == "Player")
		{
			Score -= 25;
			guiScore.text = "Points: " + Score;
			Debug.Log("you've been Hit!");
		}

Here is the code for the shooting (Taken from Tornado twins tutorial) :

	function Update()
	{
		if (LookAtTarget)
		{
			var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
			
			transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
			
			var seconds:int = Time.time;
			var oddeven = (seconds % 2);
			
			if (oddeven)
			{
			Shoot(seconds);
			}
			}
			}
			
			
		
		
	
	function Shoot(seconds)
	{
	if(seconds!=savedTime)
	{
		var bullit = Instantiate (bullitPrefab ,transform.Find("Barrel").transform.position ,Quaternion.identity);
		
		bullit.rigidbody.AddForce(transform.forward * speed);
		
		Destroy(bullit.gameObject, 3);
		
		savedTime = seconds;

Any ideas guys?

/
/
/
guiScore.text = "Points: " + Score.ToString();
/
/
/

I’m sure that not my issue but the compiler wont except that anyway Error: Operator +' cannot be applied to operands of type string’ and `method group’

Is Score a variable or a method? Usually this means its not a variable.

yes i have it set as an int variable

public int Score = 0;

Is the score var in the same script as the hit detection??

Post the full code.

One of my favorite patterns to use for score keeping is to create a Player component and add it to a gameObject in the scene. Then give the Player static access and make “score” a property. What you end up typing, if you want to increase the score by 10, would be Player.score += 10; Static access means you don’t have to drag and drop anything, or find anything in the scene, you have simple direct access. Having score as a property means you can run other code, such as a delegate for the GUI so it automatically updates the GUI when you add to the score.

I hope this gives you some ideas.

maybe it’s best if you use a static variable also if you want to keep the score from times played you probably want the PlayerPrefs function.

Oops I forgot to mention that. The other reason I use a property is so I can set player prefs in the property setter.