- Home /
Decrementing UI Text for Items remaining
I have a scene with a simple player walking around picking up items. I can achieve this, but when he hits an item I want my Text UI to decrement each time. I have looked this up in other articles and tried their methods, but none have worked for me. Can you tell me why the code below is not decrementing with each pick up?
public int remaining = 3;
public Text enemyText;
void Update()
{
enemyText.text = (remaining).ToString("0");
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
remaining--;
//--remaining;
//remaining = -1;
enemyText.text = remaining.ToString();
}
}
I would really appreciate if someone could explain to me what I am doing wrong. I have tried examples found elsewhere on Unity Answers and none (such as above) seem to be work.
Try changing line 7 to: enemyText.text = remaining.ToString();
Are you sure the
OnTriggerEnter
method is called? (Put aDebug.Log
outside of the condition to find out)Are you sure the condition is true (put a
Debug.Log
inside to find out
Note: The line in Update
is not necessary and can be moved to Start
since you update the text in your OnTriggerEnter
Answer by REDCRAFT123 · Mar 08, 2021 at 07:03 AM
I don't think you need to update your text every frame. And also OnTriggerEnter already update your text when hit something. Try to remove update function.
So I removed my code from Update and moved it to Start. This maintained it so that it displayed to screen, but --remaining won't decrement the text and -=remaining created an error saying "invalid expression term". Also, I added the Debug.Log and I got a contradicting the result; I have a SetActive on my pickup item and when I trigger it there is no problem and it does become inactive...yet there is no Debug.Log message in my console. How can it work to set the item to inactive, but not register the Debug.Log message? How do I resolve the inability to decrement?
I am not sure if I am doing this right, but was wondering if you could help me out with figuring out the decrementing.
is ur game 2d or 3d? You could be using the wrong void, if ur game is 2D try using onTriggerEnter2D. Also did you add "using UnityEngine.UI;"?
My game is 3D. What do you mean by the wrong void? I am using the "using UnityEngine.UI". I am not having trouble displaying the text, I am having trouble making the number decrement on hitting into it/colliding. Any idea why that might be?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class test : MonoBehaviour
{
public int remaining = 3;
public Text enemyText;
private void Start()
{
enemyText.text = remaining.ToString();
}
void OnTriggerEnter(Collider other)
{
Debug.Log("Player hit something");
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
remaining--;
enemyText.text = remaining.ToString();
Debug.Log(remaining);
Debug.LogWarning(remaining);
Debug.LogError(remaining);
}
}
}
This works for me. Try this
Any thoughts on my answer below? Any idea why this works for you, but does not for me @REDCRAFT123 ?
Answer by PolymathicIndustries · May 01, 2021 at 04:48 PM
I'm using the free Unity Starter kit and using their 3rd person to walk around a plane to collide with spheres and hopefully invoke the enemy remaining script. For some reason, it is not working at all. I am also using URP (glowing spheres), but I am still new to Unity so not sure if that changes anything more than effects. I did try your script and nothing happened. I checked the console and had 505 errors, all to do with the Starter Kit, not the remaining enemy script. The errors vary, but it was mostly things like:
NullReferenceException: Object reference not set to an instance of an object
EnemyAI.EnemyMove () (at Assets/StarterKit-MoveCamAI/StarterKit/Scripts/Enemy/EnemyAI.cs:81)
EnemyAI.FixedUpdate () (at Assets/StarterKit-MoveCamAI/StarterKit/Scripts/Enemy/EnemyAI.cs:42)
Is this a URP error or am I doing something wrong?
Your answer
Follow this Question
Related Questions
Picking up/Holding objects? 4 Answers
Help with adding power ups to game 1 Answer
Ammo Pickup 0 Answers
Weapon Pickup and change 2 Answers
Java for android script help??? 0 Answers