0
\$\begingroup\$

I need to use a variable from the main script of a game which is the score variable public int scoreCount; I need it in the main script because it will be needed to update the score when they get past a certain point.

I need to be able to access this variable.. so add or subtract from it i.e. scoreCount++; however I am not sure how to do this, I have looked at tutorials and they make no sense for me.. could someone please explain it in more detail.

I also need to be able to use the public void UpdateScore() function from the mainscript too, do I just do it the same way as the variable or is there a different method. Thank you.

\$\endgroup\$
2
  • \$\begingroup\$ For both of those you'd need your other script to get a reference to your main script. You can expose this reference as a public MainScript main; on the second script, then drag your main script instance into it in the inspector to wire it up in advance (if they're both in the scene or the MainScript is in a prefab). Or, you can use things like GameObject.Find(name) / FindObjectOfType(typeof(MainScript)) although these are slow to be using frequently, or use a Singleton pattern - do you need help with any of those? \$\endgroup\$
    – DMGregory
    Jan 19 '16 at 2:26
  • 1
    \$\begingroup\$ (As a sidenote, while looking up those find method names I noticed at least three answers to your question on the first page of Google results - search engines and tutorials are your friends for beginner questions like this) \$\endgroup\$
    – DMGregory
    Jan 19 '16 at 2:31
0
\$\begingroup\$

The old Unity docs have some examples. And the Unity Answers site is generally a better place to look for such things. But...

Basically, you need to reference the other gameobject and use GetComponent<YourScript>()

So, in the script your working on, you need to declare a public variable that you can assign in the inspector, or you need to use GameObject.Find("OtherGameObjectByName");

If you look at this setup, you can see two gameobjects with each with there own component/script attached. I am using this to illustrate the following code.

unity example

The 'ControlObject.cs' attached to 'GameObjectController'.

public class ControlObject : MonoBehaviour {

    // There is a better place for this, other than the Start method.
    // but I'm lazy.
    void Start () {

        // This will look for a specific game object.
        GameObject mygo = GameObject.Find("GameObjectOther");
        // if that game object is found...
        if(mygo){

             // you can access any of its components.
             DynamicObject otherguy = mygo.GetComponent<DynamicObject>();

             // Now you can modify public properties or call methods
             otherguy.scoreCount++;
        }
    }
}

The 'DynamicObject.cs' attached to 'GameObjectOther'.

public class DynamicObject : MonoBehaviour {

    //make sure you have a public propertey
    public int scoreCount = 0;

    // 
    void Update () {
        //Do something with scoreCount.

    }
}
\$\endgroup\$
0
\$\begingroup\$

If it is not necessary to create score variable in your main script and you want to access score even after changing the scene then you can use Static variables and access it anywhere, as they don't destroy through out the app life.

Make a class named Globals.cs (could be any name) and declare variable there like,

public static int SCORE = 0;

Save score to this variable before changing the scene, like,

Globals.SCORE = score;

And get it into another scene like,

int score = Globals.SCORE;

NOTE: Don't forget to reset Globals.SCORE to 0 at the start of GameScene

//==================== OTHERWISE ======================// 

You can access your main script variable if you are at the same scene by using FindObjectOfType.

Assuming that your MainScript.cs has a public variable.

public class MainScript.cs : MonoBehaviour
{
    public int _score = 0; // Initial value
}

You can get it in second script by,

public class SecondScript : MonoBehaviour
{
    MainScript _mainScript;

    void Start()
    {
        // Getting main script component from hierarchy
        _mainScript = FindObjectOfType<MainScript>();

        // Now you can access your _score variable
        Debug.Log("Score: " + _mainScript._score.ToString());
    }
}
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.