Search Unity

adding score on collision not working

Discussion in 'Scripting' started by foxvalleysoccer, Sep 5, 2015.

  1. foxvalleysoccer

    foxvalleysoccer

    Joined:
    May 11, 2015
    Posts:
    108
    I'm lost at what is going wrong here. Why isn't my score writing out in the debug log correctly.
    I see the "Hit" but my counter won't increment.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Text;
    4. using UnityEngine.UI;
    5.  
    6. public class MoveEnemy : MonoBehaviour {
    7.     //public GUIText Score;
    8.     public Text text;
    9.     public int scoreQty = 0;
    10.  
    11.     public GameObject Player;
    12.     public float velocity = -1.0f;
    13.     void Start () {
    14.         //text = GetComponent <Text>();
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void FixedUpdate () {
    19.    
    20.         GetComponent<Rigidbody2D> ().velocity = -Vector2.right;
    21.         //GetComponent<Rigidbody2D>().velocity = new Vector2 (-velocity, GetComponent<Rigidbody2D>().velocity.y);
    22.  
    23.     }
    24.  
    25.     void OnTriggerEnter2D(Collider2D other){
    26.         if (other.gameObject.CompareTag ("Player")) {
    27.             Destroy (other.gameObject);
    28.             Debug.Log ("Hit");
    29.             Application.LoadLevel ("GameOver");
    30.  
    31.         } else if (other.gameObject.CompareTag ("GivePointsBar")) {
    32.             Destroy (gameObject);
    33.             scoreQty = scoreQty+10;
    34.             Debug.Log (scoreQty);
    35.             Debug.Log("hit");
    36.         }
    37.         text.text ="Score:asdf "+scoreQty;
    38.     }
    39.  
    40.  
    41. }
    42.  
    thanks for the help.
     
  2. Lentaq

    Lentaq

    Joined:
    Apr 8, 2015
    Posts:
    57
    Well, your scoreQty variable is based in this script, and you are destroying the gameObject this script is attached to, so you could be wiping it out with your Destroy call.

    I'd recommend putting your scores somewhere else in a separate score script and just referencing it to increment your scores.