I have a script for killing AI jets and everything works except what should be the simplest part - the score keeper.
It gives a point for the first kill, then goes bonkers, and starts adding a point (or sometimes 2, 3, or 4) for every mouse click, whether I click on a plane or not… The score keeps going up until, at a different number each time, it will stop adding points, even if I kill another plane.
This is what I have:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.Vehicles.Aeroplane;
public class JetKill : MonoBehaviour {
public AeroplaneController Jet;
public int Jethealth = 10;
public GameObject JetFire;
public GameObject JetFireball;
public GameObject JetExploSound;
public GameObject JetSoundToStop;
public static int score = 0;
void Update()
{
if(Input.GetMouseButtonDown(0) == true)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit) && this.gameObject.Equals (hit.transform.gameObject))
{
Jethealth -= 5;
}
if (Jethealth <= 5) {
JetFire.SetActive (true);
}
if (Jethealth == 0) {
JetExploSound.SetActive (true);
JetFireball.SetActive (true);
JetSoundToStop.SetActive (false);
Jet.Immobilize ();
score +=1;
}
}
}
void OnGUI() {
GUI.Label (new Rect (2, 2, 222, 30), "Kills: " + score);
}
}
If anyone has encountered (and solved) this problem, or can detect what is wrong with my script, I’d be much obliged if you’d share your wisdom.
Cheers!