Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 robbos729 · Jul 27, 2019 at 11:17 PM · uitexttimerscorebool

How can i link my code together so the score value affects the timer value

hello! In my game i want to link the timer and score scripts together so for every 25 points the timer value loses 0.25 seconds off of it. How can i link both these scripts together? ScoreText controls the text of Score and NewBest is simply for the high score

Here is "KeepScoreManager" (Score script)

 using UnityEngine;
 using UnityEngine.UI;
 
 public class KeepScoreManager : MonoBehaviour
 {
     public Text ScoreText;
     private int score;
     public Text NewBest;
 
 
     public void IncreaseScore(int increment)
     {
        NewBest.text = PlayerPrefs.GetInt("NewBest").ToString();
 
         score += increment;
         if (score > PlayerPrefs.GetInt("NewBest"))
         {
             PlayerPrefs.SetInt("NewBest", score);
             NewBest.text = PlayerPrefs.GetInt("NewBest").ToString();
         }
 
         ScoreText.text = score.ToString();
     }
 }

And here is the timer script, (a lot of it towards the bottom is for game over screens and other functions within the game)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class CountdownScript : MonoBehaviour
 {
     [SerializeField] private Text uiText;
     [SerializeField] private float mainTimer;
     [SerializeField] private GameObject panel;
 
     void Start()
     {
         timer = mainTimer;
     }
 
     private float timer;
     private bool canCount = true;
     private bool doOnce = false;
 
     void Update()
     {
         if (timer >= 0.0f && canCount)
         {
             timer -= Time.deltaTime;
             uiText.text = timer.ToString("F");
         }
 
         else if (timer <= 0.0f && !doOnce)
         {
             canCount = false;
             doOnce = true;
             uiText.text = "0.00";
             timer = 0.0f;
             panel.SetActive(true);
         }
     }

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by GlassesGuy · Jul 28, 2019 at 12:05 AM

make a gameobject and call it whatever you want, then you can use GetComponent<>() to get access to your other script. Here's an example:

 public class KeepScoreManager : MonoBehavior
 {
     public gameObject countDownGameobject;
     public float threshhold = 25;
     public float scoreDecrement = 0.25
     private float relativeScore;
     private int scoreDecrementCounter;
 
     
     void Update()
     {
          relativeScore = score - threshold * scoreDecrementCounter;
          if(threshhold >=  score)
          {
               relativeScore = 0;
               countDownGameobject.GetComponent<CountdownScript>().timer -= scoreDecrement;
               scoreDecrementCounter ++;
          }
     }
 }

Hope this helps!

Comment
Add comment · Show 7 · 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 robbos729 · Jul 28, 2019 at 12:09 AM 0
Share

@GlassesGuy So public game object countdown game object is the timer, and lets say the new component is called decrement I'd put in the script GetComponent() and then we'd be done?

avatar image robbos729 · Jul 28, 2019 at 09:15 AM 0
Share

In the code I have man game issues 1)the -.25 decrement says cant be converted into a float, use an F suffix 2) the name threshhold doesnt exist in this context 3) countdownscript.timer isnt accesible because of its protection level

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

 public class Decrement : $$anonymous$$onoBehaviour
  {
     public GameObject Score;
      public GameObject Timer;
      public float threshhold = 25;
      public float scoreDecrement = 0.25;
      private float relativeScore;
      private int scoreDecrementCounter;
  
      
      void Update()
      {
           relativeScore = Score - threshhold * scoreDecrementCounter;
           if(threshhold >=  Score)
           {
                relativeScore = 0;
                Timer.GetComponent<CountdownScript>().Timer -= scoreDecrement;
                scoreDecrementCounter ++;
           }
      }
  }




avatar image GlassesGuy robbos729 · Jul 29, 2019 at 10:36 PM 0
Share

Yes! Sorry I frequently make mistakes with code when I'm trying to go quick. Every time 0.25 is used it should be converted to 0.25f so that the game knows it's a float and not something else. On the "public float scoreDecrement = 0.25" part you need to add a semicolon on the end. On whatever gameobject you have $$anonymous$$eepScore$$anonymous$$anager script you need to set the gameobject in the editor, Just go to that gameobject and drag the gameobject with your Countdown script and drag it in. and you lastly need to change the "private float timer;" code in your Countdown script to "public float timer;" so that other scripts can access it. Sorry for the confusion, tbh i'm still learning C# and finding problems on these forums is a way for me to learn code better by solving problems I don't already know how to solve.

avatar image robbos729 GlassesGuy · Jul 30, 2019 at 09:08 AM 0
Share

So how does it know what the score is because if we don't need it in the code then how does it know as one issue is I can't do the score

Show more comments

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

191 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 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

Which type of Variable do I need to use to change the Value of a text UI object ? 0 Answers

Can't drag text object into inspector? 0 Answers

Want my UI to not move when I move my camera? 2 Answers

UI Problem for Game 1 Answer

Making a Text Timer and Score Display 2 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