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. Dismiss Notice

Need one script to pull variable data from another script on same object

Discussion in 'Scripting' started by UnderworldGames, Nov 10, 2020.

  1. UnderworldGames

    UnderworldGames

    Joined:
    Oct 26, 2020
    Posts:
    11
    I have two scripts affecting my player: 1. Score script to increase score, and 2. Player script that controls movement/sound. I am trying to get Player script to access the int score from the Score script so that I can increase player speed based on the user's current score. I assumed that I would need a line in Player script like:

    playerSpeed = playerSpeed + score * 10;

    I can't get the 'score' in that line of code to access the score from the other script. Is this the right line of code, and how can I get the score to be copied from a different script?

    Score Script
    Code (CSharp):
    1. public class Score : MonoBehaviour
    2. {
    3.     public int score;
    4.     public int highScore;
    5.     public TextMeshProUGUI scoreUI;
    6.     public TextMeshProUGUI highScoreUI;
    7.  
    8.     void Start()
    9.     {
    10.         highScore = PlayerPrefs.GetInt("highscore");
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         scoreUI.text = score.ToString();
    17.         highScoreUI.text = highScore.ToString();
    18.         if (score > highScore) {
    19.             highScore = score;
    20.             PlayerPrefs.SetInt("highscore", score);
    21.         }
    22.     }
    Player Script
    Code (CSharp):
    1. public class Player : MonoBehaviour
    2. {
    3.     public GameObject sceneManager;
    4.     public float playerSpeed = 1500;
    5.     public float directionalSpeed = 20;
    6.     public AudioClip scoreUp;
    7.     public AudioClip damage;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.        
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         //playerSpeed = playerSpeed + score * 10; (need to get "+ score" to interact with score script)
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Do something like this, and set the reference in the inspector.

    Code (CSharp):
    1. public class Player : MonoBehaviour
    2. {
    3.     public GameObject sceneManager;
    4.     public float playerSpeed = 1500;
    5.     public float directionalSpeed = 20;
    6.     public AudioClip scoreUp;
    7.     public AudioClip damage;
    8.     public Score ScoreComponent;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.    
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         playerSpeed = playerSpeed + ScoreComponent.score * 10; //(need to get "+ score" to interact with score script)
    20.     }
    Though Update is called once per frame, so playerSpeed is going to skyrocket extremely quickly like this. But that's not your question.
     
  3. UnderworldGames

    UnderworldGames

    Joined:
    Oct 26, 2020
    Posts:
    11
    So I implemented this change, and after saving the script nothing new appeared in the Inspector and I got the error message CS0246 "The type or namespace 'score' could not be found (are you missing a using directive or an assembly reference?)"
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The type should be written as "Score" not "score". It is just the name of the class from your other script. It has to be typed exactly. Different capitalization is the same as different spelling in C#. If you're still having trouble, please post both scripts again in their current state.
     
  5. UnderworldGames

    UnderworldGames

    Joined:
    Oct 26, 2020
    Posts:
    11
    Okay..... I'm like 90% certain that I had it capitalized correctly in the first place.... but that seemed to fix it. Now you're right about the void Update problem too, so how can I make sure the the playerSpeed stays where it should? So it starts at 1500, if you have one point it should stay at 1510, then 1520 with two points etc.
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If you want to recalculate every frame, store the base speed separately, then do your multiplication with score, and store that in its own variable. Whenever score hasn't changed, the resulting speed will be the same each frame. Whenever score increases between frames then the resulting speed will also increase.

    Code (CSharp):
    1. public class Player : MonoBehaviour
    2. {
    3.     public GameObject sceneManager;
    4.     public float playerSpeed = 1500f;
    5.     public float baseSpeed = 1500f;
    6.     public float directionalSpeed = 20f;
    7.     public AudioClip scoreUp;
    8.     public AudioClip damage;
    9.     public Score ScoreComponent;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.  
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         playerSpeed = baseSpeed + ScoreComponent.score * 10; //(need to get "+ score" to interact with score script)
    21.     }
     
  7. UnderworldGames

    UnderworldGames

    Joined:
    Oct 26, 2020
    Posts:
    11

    This did it! Thank you so much for your help!
     
    Joe-Censored likes this.