I was looking at the Space Shooter Tutorial, and I tried to implement their score script into my game. I seem to have the display score of 0, but it is not increasing with every prefab I destroy. What is the problem? Here are the scripts: GameController: Code (csharp): var hazard : GameObject; var spawnValues : Vector3; var hazardCount : int; var spawnWait : float; var startWait : float; var waveWait : float; var scoreText : GUIText; private var score : int; function Start () { score = 0; UpdateScore (); StartCoroutine (SpawnWaves ()); } function SpawnWaves () { yield WaitForSeconds (startWait); while (true) { for ( var i : int= 0; i < hazardCount; i++) { var spawnPosition : Vector3= new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z); var spawnRotation : Quaternion= Quaternion.identity; Instantiate (hazard, spawnPosition, spawnRotation); yield WaitForSeconds (spawnWait); } yield WaitForSeconds (waveWait); } } function AddScore (newScoreValue : int) { score += newScoreValue; UpdateScore (); } function UpdateScore () { scoreText.text = "Score: " + score; } DestroyByContact: Code (csharp): var explosion : GameObject; var playerExplosion : GameObject; var scoreValue : int; private var gameController : GameController; function Start () { var gameControllerObject : GameObject = GameObject.FindWithTag ("GameController"); if (gameControllerObject != null) { gameController = gameControllerObject.GetComponent (GameController); } if (gameController == null) { Debug.Log ("Cannot find 'GameController' script"); } } function OnTriggerEnter(other : Collider) { if (other.tag == "Boundary") { return; } Instantiate(explosion, transform.position, transform.rotation); if (other.tag == "Player") { Instantiate(playerExplosion, other.transform.position, other.transform.rotation); } gameController.AddScore (scoreValue); Destroy(other.gameObject); Destroy(gameObject); } DestroyOther: Code (csharp): #pragma strict function Update () { if(Input.GetKey(KeyCode.Space)) { Destroy(gameObject); } } Player: Code (csharp): function Update () { if (Input.GetMouseButtonDown(0)) { var hitm : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); if(hitm.collider != null) { Destroy (hitm.collider.gameObject); } if (Input.touchCount > 0 Input.GetTouch(0).phase == TouchPhase.Began){ if(hitm.collider != null) { Destroy (hitm.collider.gameObject); } } } }
I dont know the Space Shooter Tutorial, but from what I see, your scoreValue variable never seems to be set to anything before you add it to the score.
you have : var scoreValue : int; But you dont give it a value. ( like scoreValue = 100 ) Try to print NewScoreValue in your AddScore function to see the value you are trying to add to score
On line 30 of your DestroyByContact script you have: Code (csharp): gameController.AddScore (scoreValue); This calls the AddScore function from your gameController, passing it the scoreValue from this current script - but I don't see anywhere that scoreValue is set to a number. So unless scoreValue is set to something in the inspector for the object this script is attached to, you'll be adding 0 to the score.
Basically I will be replacing "newScoreValue" from line 33 of my GameController script with a number, which in this case would be 1. Code (csharp): score += 1; Correct?
While that would work, you'd then have a function that takes a parameter (newScoreValue) and doesn't actually do anything with it, which isn't a good habit to get into. Instead, you should look in the inspector window of any objects you have your DestroyByContact script on and set the value of scoreValue there.
I just added a scoreValue of 1: It's showing the score display, but it's still zero every time I destroy a star.
I'm assuming you've set that value in the prefab for the Star and not just in once instance of it? If you have, then to be honest, I'm not sure what's going on. I don't see anything obviously wrong from the code you've posted.
Thanks. Sadly, I dropped those scripts. I found this one after searching through Unity's Answers: Code (csharp): var score : int = 0; function Update () { if (Input.GetMouseButtonDown(0)) { var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; if (Physics.Raycast (ray, hit)) { print ("Hit something: "+hit.collider.name); if (hit.collider.name == "Cube") { score += 1; Debug.Log("Scored again! :"+score); Destroy(hit.collider.gameObject); } } } } I tested the script by placing cubes within the scene, and it worked. I can see the score increase by one every time I destroyed a cube. But, it seems to not display the score, and it does not work with my Star Sprite, which is being spawned by a GameController. What do I do?
Assuming you placing that code in it's own script, it's updating a local variable for score and not the one in your gameController, so you need to replace line 11 with a call to AddScore from your gameController. To do that, you'll need a reference to you gameController in the same way you do in the Start () function of DestroyByContact. As for the box / star issue, line 10 is testing explicitly for collisions against "Cube" - you'll need to modify this to test for you Stars.
Does this seem correct: Code (JavaScript): var score : int = 0; public var gameController : GameController; function Start () { var gameControllerObject : GameObject = GameObject.FindWithTag ("GameController"); if (gameControllerObject != null) { gameController = gameControllerObject.GetComponent (GameController); } if (gameController == null) { Debug.Log ("Cannot find 'GameController' script"); } } function Update () { if (Input.GetMouseButtonDown(0)) { var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; if (Physics.Raycast (ray, hit)) { print ("Hit something: "+hit.collider.name); if (hit.collider.name == "Cube") { Debug.Log("Play again! :"+score); Destroy(hit.collider.gameObject); } } } }