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): using UnityEngine; using System.Collections; using UnityEngine.UI; public class TriggerZone : MonoBehaviour { bool pontos; public Text MyText; public int score; // Use this for initialization void Start () { MyText.text = "0"; score = 0; } void OnTriggerEnter (Collider other) { score = score + 1 ; MyText.text = score.ToString(); Destroy (gameObject, 1); } void Update(){ } }
Line 24 above destroys the GameObject this script is located on, and hence this script. Is that why subsequent ones don't change?
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.
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.
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): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class variavelnotas : MonoBehaviour { public static int notasvalor; public Text notaspegas; // Use this for initialization void Start () { notasvalor = 0; notaspegas.text = notasvalor.ToString (); } void OnTriggerEnter (Collider coll){ notasvalor++; notaspegas.text = notasvalor.ToString (); } // Update is called once per frame void Update () { } }
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!