Search Unity

Score Not Adding Up..

Discussion in 'Scripting' started by Zypher, May 31, 2014.

  1. Zypher

    Zypher

    Joined:
    Apr 7, 2014
    Posts:
    36
    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):
    1. var hazard : GameObject;
    2. var spawnValues : Vector3;
    3. var hazardCount : int;
    4. var spawnWait : float;
    5. var startWait : float;
    6. var waveWait : float;
    7.  
    8. var scoreText : GUIText;
    9. private  var score : int;
    10.  
    11. function Start () {
    12.     score = 0;
    13.     UpdateScore ();
    14.     StartCoroutine (SpawnWaves ());
    15. }
    16.  
    17. function SpawnWaves () {
    18.     yield WaitForSeconds (startWait);
    19.     while (true)
    20.     {
    21.         for ( var i : int= 0; i < hazardCount; i++)
    22.         {
    23.              var spawnPosition : Vector3= new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    24.              var spawnRotation : Quaternion= Quaternion.identity;
    25.             Instantiate (hazard, spawnPosition, spawnRotation);
    26.             yield WaitForSeconds (spawnWait);
    27.         }
    28.         yield WaitForSeconds (waveWait);
    29.     }
    30. }
    31.  
    32. function AddScore (newScoreValue : int) {
    33.     score += newScoreValue;
    34.     UpdateScore ();
    35. }
    36.  
    37. function UpdateScore () {
    38.     scoreText.text = "Score: " + score;
    39. }
    DestroyByContact:
    Code (csharp):
    1. var explosion : GameObject;
    2. var playerExplosion : GameObject;
    3. var scoreValue : int;
    4. private var gameController : GameController;
    5.  
    6. function Start ()
    7. {
    8.     var gameControllerObject : GameObject = GameObject.FindWithTag ("GameController");
    9.     if (gameControllerObject != null)
    10.     {
    11.         gameController = gameControllerObject.GetComponent (GameController);
    12.     }
    13.     if (gameController == null)
    14.     {
    15.         Debug.Log ("Cannot find 'GameController' script");
    16.     }
    17. }
    18.  
    19. function OnTriggerEnter(other : Collider)
    20. {
    21.     if (other.tag == "Boundary")
    22.     {
    23.         return;
    24.     }
    25.     Instantiate(explosion, transform.position, transform.rotation);
    26.     if (other.tag == "Player")
    27.     {
    28.         Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
    29.     }
    30.     gameController.AddScore (scoreValue);
    31.     Destroy(other.gameObject);
    32.     Destroy(gameObject);
    33. }
    DestroyOther:
    Code (csharp):
    1. #pragma strict
    2.  
    3. function Update ()
    4. {
    5.     if(Input.GetKey(KeyCode.Space))
    6.     {
    7.         Destroy(gameObject);
    8.     }
    9. }
    Player:
    Code (csharp):
    1. function Update () {
    2.  
    3.      if (Input.GetMouseButtonDown(0))
    4.      {
    5.  
    6.        var hitm : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    7.        if(hitm.collider != null)
    8.        {
    9.          Destroy (hitm.collider.gameObject);
    10.        }
    11.        if (Input.touchCount > 0  Input.GetTouch(0).phase == TouchPhase.Began){
    12.        if(hitm.collider != null)
    13.        {
    14.          Destroy (hitm.collider.gameObject);
    15.        }
    16.      }
    17. }
    18. }
     
  2. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    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.
     
  3. Zypher

    Zypher

    Joined:
    Apr 7, 2014
    Posts:
    36
    What do you mean?

    Sorry I'm fairly new to all of this,
     
  4. oleanna

    oleanna

    Joined:
    May 21, 2014
    Posts:
    11
    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
     
    Last edited: May 31, 2014
  5. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    On line 30 of your DestroyByContact script you have:
    Code (csharp):
    1. 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.
     
  6. Zypher

    Zypher

    Joined:
    Apr 7, 2014
    Posts:
    36
    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):
    1. score += 1;
    Correct?
     
  7. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    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.
     
  8. Zypher

    Zypher

    Joined:
    Apr 7, 2014
    Posts:
    36
    I just added a scoreValue of 1:


    It's showing the score display, but it's still zero every time I destroy a star.
     
  9. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    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.
     
  10. Zypher

    Zypher

    Joined:
    Apr 7, 2014
    Posts:
    36
    Thanks. :)

    Sadly, I dropped those scripts. I found this one after searching through Unity's Answers:
    Code (csharp):
    1. var score : int = 0;
    2.  
    3. function Update () {
    4.  
    5.     if (Input.GetMouseButtonDown(0)) {
    6.        var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    7.        var hit : RaycastHit;
    8.        if (Physics.Raycast (ray, hit)) {
    9.            print ("Hit something: "+hit.collider.name);
    10.            if (hit.collider.name == "Cube") {
    11.              score += 1;
    12.              Debug.Log("Scored again! :"+score);
    13.              Destroy(hit.collider.gameObject);
    14.              }
    15.  
    16.        }
    17.     }
    18. }
    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?
     
  11. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    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.
     
  12. Zypher

    Zypher

    Joined:
    Apr 7, 2014
    Posts:
    36
    Does this seem correct:
    Code (JavaScript):
    1. var score : int = 0;
    2. public var gameController : GameController;
    3.  
    4. function Start ()
    5. {
    6.     var gameControllerObject : GameObject = GameObject.FindWithTag ("GameController");
    7.     if (gameControllerObject != null)
    8.     {
    9.         gameController = gameControllerObject.GetComponent (GameController);
    10.     }
    11.     if (gameController == null)
    12.     {
    13.         Debug.Log ("Cannot find 'GameController' script");
    14.     }
    15. }
    16.  
    17. function Update () {
    18.     if (Input.GetMouseButtonDown(0)) {
    19.        var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    20.        var hit : RaycastHit;
    21.        if (Physics.Raycast (ray, hit)) {
    22.            print ("Hit something: "+hit.collider.name);
    23.            if (hit.collider.name == "Cube") {
    24.              Debug.Log("Play again! :"+score);
    25.              Destroy(hit.collider.gameObject);
    26.              }
    27.        }
    28.     }
    29. }