# access public variable in other script/scene

I have public variable score in script scr_a that attach to object obj_a in scene sce_a.

now I need to access the variable (score) in script scr_b that attach to object obj_b in scene sce_b.

I have try something but they did not work and I think that's because my scripts are in two different scene...

• If you load new scene Unity will destroy every object on prevoius scene. You can mark object to not be destroyed with DontDestroyOnLoad() method. – Paweł Marecki Aug 12 '16 at 6:29
• This appears to be a duplicate of Game states & passing data between scenes, but it can not be marked as such because the answer is not upvoted or accepted. – Philipp Aug 12 '16 at 12:14
• @Philipp there, have an upvote. Does the duplicate flagging work now? – Tyyppi_77 Aug 12 '16 at 14:24
• @Tyyppi_77 yes, works now. – Philipp Aug 12 '16 at 14:25

Use a Score managing object that doesn't destroy in scene load or destroy. Unity have have a method DontDestroyOnLoad() for doing that. Get this object from other script with tag like "ScoreManager" then access the public methods as you need.

Use some code like below:

private int score;

void Awake()
{
}

public int GetCurrentScore()//call this method to get the current score
{
return score;
}

public void UpdateScore()//call this method to update the score increment
{
score++;
}

• This would need more to act as a score manager. I generally use a static self reference, given that management scripts shouldn't have multiple instances running at the same time. – Gnemlock Aug 13 '16 at 2:51

You have to maintain/retain/dontDestory scr_a script when you switching from sce_a to sce_b and this can be acheiveable through this

public scr_a{
public int score;
void Awake() {
Debug.Log("DontDestory Script's Awake");
}
///your logic update or blah blah functions

}


Adding DontDestoryOnLoad in your scr_a script will maintain you scr_a script object in sce_b then you can get it in scr_b using this

scr_a scr_aObj = GameObject.FindObjectOfType<scr_a>();
Debug.Log(scr_aObj.score);

• @Timelord64 Hi... I fixed the first error. but how can i fix second error? thanks... – mmoj Aug 14 '16 at 10:19
• @Timelord64 Oops... i know that... actaully i think that was an accident – mmoj Aug 14 '16 at 12:35
• @mmoj what is the error ? – Muhammad Faizan Khan Aug 15 '16 at 4:38
• @Timelord64 whats wrong in the code, it is untested code giving the accurate concept. – Muhammad Faizan Khan Aug 15 '16 at 4:39
• There are two methods for finding an object as you describe. Object.FindObjectOfType<Type>() and Object.FindObjectOfType(). In your example, your using the method Object.FindObjectOfType<>(scr_1), which as I said, gives you two seperate errors from bad overloading. Even in the case of the later, you still need to cast the return for it to work. It is always best to test code you have not used before, or are unfamiliar with, as even minor discrepancies like this will cause fatal errors. In this example, you do not even need to run the code before the compiler should report errors. – Gnemlock Aug 15 '16 at 4:51