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 ReferenceWolf · Aug 18, 2021 at 02:07 PM · update functiontext box

how do I fix my text box not updating?

I have recently been trying to make a clicker game, and obviously in a clicker game you need some sort of score counter that shows how much of whatever your currency is. I wrote a block counter script that adds 1 to the score every time you click and it works. my text box changes from a score of 0 to 1 when i click then goes to 2 and so on as i click. I tried to make an auto score counter code that I can apply to upgrades. These next set of codes are what I use for a upgrade I called "Miner"

Code 1

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class MinerCode : MonoBehaviour
 {
 
     public GameObject FakeButton;
     public GameObject FakeText;
     public GameObject RealButton;
     public GameObject RealText;
     public int ScoreValue;
     public static int MinerValue = 20;
     public static bool TurnOffButton = false;
     public GameObject MinerStats;
     public static int MinerAmount;
     public static int MinesPerSecond;
     
     // Start is called before the first frame update
     void Start()
     {
         RealButton.SetActive(false);
     }
 
     // Update is called once per frame
     void Update()
     {
         ScoreValue = BlockCounter.ScoreValue;
         MinerStats.GetComponent<Text>().text = "MINER: " + MinerAmount + " AT " + MinesPerSecond + " BPS";
         FakeText.GetComponent<Text>().text = "MINER - Unaffordable ( " + MinerValue + " Blocks)";
         RealText.GetComponent<Text>().text = "MINER - " + MinerValue + " Blocks";
         if (ScoreValue >= MinerValue)
         {
             FakeButton.SetActive(false);
             RealButton.SetActive(true);
         }
 
         if (TurnOffButton == true) 
         {
 
             RealButton.SetActive(false);
             FakeButton.SetActive(true);
             TurnOffButton = false;
 
         }
     }
 }
 

Code 2

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class AutoBlockMiner : MonoBehaviour
 {
 
     public bool CreatingBlocks = false;
     public static int BlockIncrease = 5;
     public int InternalIncrease;
 
     // Update is called once per frame
     void Update()
     {
         BlockIncrease = MinerCode.MinesPerSecond;
         InternalIncrease = BlockIncrease;
         if (CreatingBlocks == false)
         {
             CreatingBlocks = true;
             StartCoroutine(CreateTheBlock());
         }
     }
 
     IEnumerator CreateTheBlock()
     {
         BlockCounter.ScoreValue += InternalIncrease;
         yield return new WaitForSeconds(1);
         CreatingBlocks = false;
     }
 }
 

Code 3

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PurchaseLogMiner : MonoBehaviour
 {
     public GameObject AutoBlock;
     public AudioSource PlayMinerSound;
       
     public void StartAutoBlock()
     {
         PlayMinerSound.Play();
         AutoBlock.SetActive(true);
         BlockCounter.ScoreValue -= MinerCode.MinerValue;
         MinerCode.MinerValue *= 2;
         MinerCode.TurnOffButton = true;
         MinerCode.MinesPerSecond += 5;
         MinerCode.MinerAmount += 1;
     }
 }
 

When I try to use these codes and apply them to unity, it appears to be fine, but when I play the game, the score value doesnt increase by 5 ( I used 5 because that is how much the miner is supposed to give every second when bought) until I click. In the inspector however, I can clearly see the score increase with the InternalIncrease int. How can I make this so the text box updates even when I dont click my button?

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 logicandchaos · Aug 19, 2021 at 03:20 AM 0
Share

Whenever you change the value you should update the text component, but you should also reference them as Text objects not GameObjects so you don't have to use GetComponent, it uses a lot of resources.

avatar image ReferenceWolf logicandchaos · Aug 19, 2021 at 02:59 PM 0
Share

hey there, thank you for the reply, when i get the chance Ill do this. What code would I need to add to make it update the text component?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by joan_stark · Aug 24, 2021 at 08:36 PM

Well, first, make surte cache all your text components. You can do that by

  public class MinerCode : MonoBehaviour
  {
  
      public GameObject FakeButton;
      public GameObject FakeText;
      [...]
 
      //I will do minerstats text only for example.
      private Text minerStatsText;
      
      void Start()
      {
          RealButton.SetActive(false);
          //Here we set minerStatsTextComponent:
          minerStatsText = MinerStats.GetComponent<Text>();
      }
  
      void Update()
      {
          ScoreValue = BlockCounter.ScoreValue;
          //Now here you do:
          minerStatsText.text = "MINER: " + MinerAmount + " AT " + MinesPerSecond + " BPS";
          [...]
      }
  }

And well, about the update text not working, you are not changing any time the values of Miner Amount or Mines per second, for example. You only do it once at most in StartAutoBlock. When you create a StartAutoBlock, you should add and update method that updated the value every frame, or every second in order to make it change.

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

124 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Input Problems 2 Answers

How to stop code in the update function? 1 Answer

Coroutine throught update funct 0 Answers

Weird Spring Oscillation Error 1 Answer

Input and Rigidbody (Update and FixedUpdate) 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