Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to read my score in another script

Discussion in 'Scripting' started by herman111, Oct 10, 2018.

  1. herman111

    herman111

    Joined:
    May 11, 2014
    Posts:
    119
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ScoreManager : MonoBehaviour
    7. {
    8.  
    9.     private Text myText;
    10.     private long score;
    11.  
    12.     void Start()
    13.     {
    14.         myText = this.GetComponent<Text> ();
    15.         UpDateScore ();
    16.     }
    17.  
    18.     public void AddScore(int addToScore)
    19.     {
    20.         score += addToScore;
    21.         UpDateScore ();
    22.     }
    23.  
    24.     public void SubtractScore(int addToScore)
    25.     {
    26.         score -= addToScore;
    27.         if (score < 0)
    28.         {
    29.             score = 0;
    30.         }
    31.         UpDateScore ();
    32.     }
    33.  
    34.     private void UpDateScore()
    35.     {
    36.         myText.text = "Score - " + score;
    37.     }
    38.  
    39.      
    40. }
    41.  
    in public void AddScore….how do I read and add some points to it
     
    Last edited: Oct 12, 2018
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Just set the score to public and get a reference to this instance of the script. Usually that is done with GetComponent, or established using a field with the inspector.
     
  3. herman111

    herman111

    Joined:
    May 11, 2014
    Posts:
    119
    don't want to do it that way
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If you want to return it from AddScore you just do it like this.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ScoreManager : MonoBehaviour
    7. {
    8.  
    9.     private Text myText;
    10.     private long score;
    11.  
    12.     void Start()
    13.     {
    14.         myText = this.GetComponent<Text> ();
    15.         UpDateScore ();
    16.     }
    17.  
    18.     public long AddScore(int addToScore)
    19.     {
    20.         score += addToScore;
    21.         UpDateScore ();
    22.         return score;
    23.     }
    24.  
    25.     public void SubtractScore(int addToScore)
    26.     {
    27.         score -= addToScore;
    28.         if (score < 0)
    29.         {
    30.             score = 0;
    31.         }
    32.         UpDateScore ();
    33.     }
    34.  
    35.     private void UpDateScore()
    36.     {
    37.         myText.text = "Score - " + score;  
    38.     }
    39.  
    40.      
    41. }
    42.  
     
  5. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    Your code is fine, when you send an amount to AddScore, it will do that and then update the text... IS this not what you want?

    Unless there is some issue with adding a long and and int, in which you would add (long) to the value to convert it.. e.g. (long)amount

    Also,
    Here is a tidy version of your code. You can send through add and subtract in the same function just by calling a negative number instead for lowering the score.

    50 += -25 would equal 25 see.

    Code (CSharp):
    1. public class ScoreManager : MonoBehaviour
    2. {
    3.     private Text myText;
    4.     private long score;
    5.     void Start()
    6.     {
    7.         myText = this.GetComponent<Text> ();
    8.         UpDateScoreText();
    9.     }
    10.  
    11.     public void AmendScore(int amount)
    12.     {
    13.         score += addToScore;
    14. score = mathF.clamp(0, int.MaxValue);
    15.         UpDateScoreText();
    16.     }
    17.     private void UpDateScoreText()
    18.     {
    19.         myText.text = "Score - " + score;
    20.     }    
    21. }
     
    Last edited: Oct 10, 2018
  6. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    You can use a singleton and make everything static so that you don’t actually need an instance of this object to modify the score. Then have your text ui object do a check if the score has changed each frame.
     
  7. herman111

    herman111

    Joined:
    May 11, 2014
    Posts:
    119
    This is all fine and thanks...but my question is still unanswered....how do I send addToScore = 10; to the AddScore.
     
  8. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    It’s not unanswered: you simply make your variables and method static.
     
  9. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    AddScore(10);
    I'm sure this cant be the question your asking, its way to easy?
    If it isn't, in your next post can you try and be more clear about what you want, and use some terminology.
     
  10. herman111

    herman111

    Joined:
    May 11, 2014
    Posts:
    119
    I'll try this again…...How do I access this score manager script from another script...say a enemy script....what line of code do I use to add points to the score manager script?
     
  11. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    Your message come across a quite arrogant dude!
    I'm still gonna help you though because I do this for me :rolleyes:



    Every script is publicly accessible. The content may not be however. Here is an example
    Code (CSharp):
    1. public class ScoreManager
    2. {
    3.     public void AddScore();
    4. }
    5.  
    6. // From another Script you can access AddScore like this..
    7. ScoreManager _scoreManager = new ScoreManager();
    8. _scoreManager.AddScore();
    9. //Note this creates a new instance and is not referencing the script you have attached to an object in the scene.
    10.  
    11.  
    12. public class ScoreManager
    13. {
    14.     public static void AddScore();
    15. }
    16.  
    17. //Note AddScore is now static.
    18. //This means if you have this script attached to several object, and call add score then it will run on all objects.
    19. //Used like this
    20. ScoreManager.AddScore();
     
  12. herman111

    herman111

    Joined:
    May 11, 2014
    Posts:
    119
    Sorry if I came across that way...i'm new at this and find it hard to explain things....BTW i'm 70 so a little break please.
     
  13. Deleted User

    Deleted User

    Guest

    And it helps me! :)