Search Unity

add a score to text

Discussion in 'Scripting' started by lucasak74, Jan 10, 2020.

  1. lucasak74

    lucasak74

    Joined:
    Feb 8, 2018
    Posts:
    4
    i'm trying to do a score but it only works with the first object but the others the text doesn't change and keep with number 1, i'll be happy with any help

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class TriggerZone : MonoBehaviour {
    6.  
    7.  
    8.     bool pontos;
    9.     public Text MyText;
    10.     public int score;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.  
    15.         MyText.text = "0";
    16.         score = 0;
    17.     }
    18.  
    19.  
    20.     void OnTriggerEnter (Collider other) {
    21.  
    22.         score = score + 1 ;
    23.         MyText.text = score.ToString();
    24.         Destroy (gameObject, 1);
    25.  
    26.     }
    27.  
    28.     void Update(){
    29.    
    30.  
    31.     }
    32.  
    33. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Line 24 above destroys the GameObject this script is located on, and hence this script.

    Is that why subsequent ones don't change?
     
    Joe-Censored likes this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Each TriggerZone instance is tracking its own score separately. So all of them start at 0, and when OnTriggerEnter gets called they add 1 to 0 and set that to the Text component, then destroy their own GameObject. I don't see how you can get above 1 doing it like this. You probably want to have a central script which tracks the score in one place, or set the score to be static so it is shared across all instances of this script.
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Yes, what Uncle Joe says above... look into tutorials on Game Managers, a broad term used to describe a persistent central construct that tracks everything related to a game in progress. There are plenty of examples on Youtube to give you a feel for what is involved.
     
    Joe-Censored likes this.
  5. lucasak74

    lucasak74

    Joined:
    Feb 8, 2018
    Posts:
    4
    i made one that worked for me, i put on my player prefab instead of the object, now the variable stays, i'm learning about scripts so sorry to y'all

    that's what i made


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6.  
    7.  
    8. public class variavelnotas : MonoBehaviour {
    9.  
    10.     public static int notasvalor;
    11.     public Text notaspegas;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.  
    16.         notasvalor = 0;
    17.         notaspegas.text = notasvalor.ToString ();
    18.     }
    19.  
    20.     void OnTriggerEnter (Collider coll){
    21.  
    22.         notasvalor++;
    23.         notaspegas.text = notasvalor.ToString ();
    24.  
    25.     }
    26.  
    27.  
    28.     // Update is called once per frame
    29.     void Update () {
    30.      
    31.     }
    32. }
    33.  
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Excellent, and no need to be sorry... that's the point! Heck, I've been doing this for four decades and I'm still 'learning about scripts...' Just ask my QA department guys when they're checking over my code!
     
    lucasak74 likes this.