Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PolymathicIndustries · Mar 07, 2021 at 09:40 PM · pickupincrement

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pil95 · Mar 07, 2021 at 10:00 PM -1
Share

Try changing line 7 to: enemyText.text = remaining.ToString();

avatar image Hellium · Mar 07, 2021 at 10:04 PM 1
Share
  1. Are you sure the OnTriggerEnter method is called? (Put a Debug.Log outside of the condition to find out)

  2. 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PolymathicIndustries · Mar 20, 2021 at 05:07 PM 0
Share

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?

avatar image PolymathicIndustries · Apr 03, 2021 at 04:51 PM 0
Share

I am not sure if I am doing this right, but was wondering if you could help me out with figuring out the decrementing.

avatar image SancleMente PolymathicIndustries · Apr 03, 2021 at 08:17 PM 0
Share

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;"?

avatar image PolymathicIndustries SancleMente · Apr 07, 2021 at 02:49 PM 0
Share

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?

Show more comments
Show more comments
avatar image REDCRAFT123 · Apr 20, 2021 at 11:29 AM 0
Share

alt text

alt text

 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

screenshot-2021-04-20-192352.png (154.5 kB)
screenshot-2021-04-20-192506.png (68.2 kB)
avatar image PolymathicIndustries REDCRAFT123 · May 10, 2021 at 07:26 PM 0
Share

Any thoughts on my answer below? Any idea why this works for you, but does not for me @REDCRAFT123 ?

avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

116 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges