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

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

public class Score : MonoBehaviour
{
    public int score;
    public int highScore;
    public TextMeshProUGUI scoreUI;
    public TextMeshProUGUI highScoreUI;

    void Start()
    {
        highScore = PlayerPrefs.GetInt("highscore");
    }

    // Update is called once per frame
    void Update()
    {
        scoreUI.text = score.ToString();
        highScoreUI.text = highScore.ToString();
        if (score > highScore) {
            highScore = score;
            PlayerPrefs.SetInt("highscore", score);
        }
    }

Player Script

public class Player : MonoBehaviour
{
    public GameObject sceneManager;
    public float playerSpeed = 1500;
    public float directionalSpeed = 20;
    public AudioClip scoreUp;
    public AudioClip damage;

    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        //playerSpeed = playerSpeed + score * 10; (need to get "+ score" to interact with score script)

Do something like this, and set the reference in the inspector.

public class Player : MonoBehaviour
{
    public GameObject sceneManager;
    public float playerSpeed = 1500;
    public float directionalSpeed = 20;
    public AudioClip scoreUp;
    public AudioClip damage;
    public Score ScoreComponent;

    // Start is called before the first frame update
    void Start()
    {
    
    }

    // Update is called once per frame
    void Update()
    {
        playerSpeed = playerSpeed + ScoreComponent.score * 10; //(need to get "+ score" to interact with score script)
    }

Though Update is called once per frame, so playerSpeed is going to skyrocket extremely quickly like this. But that’s not your question.

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?)”

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.

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.

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.

public class Player : MonoBehaviour
{
    public GameObject sceneManager;
    public float playerSpeed = 1500f;
    public float baseSpeed = 1500f;
    public float directionalSpeed = 20f;
    public AudioClip scoreUp;
    public AudioClip damage;
    public Score ScoreComponent;

    // Start is called before the first frame update
    void Start()
    {
  
    }

    // Update is called once per frame
    void Update()
    {
        playerSpeed = baseSpeed + ScoreComponent.score * 10; //(need to get "+ score" to interact with score script)
    }

This did it! Thank you so much for your help!

1 Like