-1
\$\begingroup\$

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

\$\endgroup\$
4
  • \$\begingroup\$ If you load new scene Unity will destroy every object on prevoius scene. You can mark object to not be destroyed with DontDestroyOnLoad() method. \$\endgroup\$ Aug 12, 2016 at 6:29
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – Philipp
    Aug 12, 2016 at 12:14
  • \$\begingroup\$ @Philipp there, have an upvote. Does the duplicate flagging work now? \$\endgroup\$
    – user35344
    Aug 12, 2016 at 14:24
  • \$\begingroup\$ @Tyyppi_77 yes, works now. \$\endgroup\$
    – Philipp
    Aug 12, 2016 at 14:25

2 Answers 2

0
\$\begingroup\$

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()
{
 DontDestroyOnLoad(this.gameObject);
}

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++;
}
\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – Gnemlock
    Aug 13, 2016 at 2:51
-1
\$\begingroup\$

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");
        DontDestroyOnLoad(this);
    }
///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);
\$\endgroup\$
8
  • \$\begingroup\$ @Timelord64 Hi... I fixed the first error. but how can i fix second error? thanks... \$\endgroup\$
    – mmoj
    Aug 14, 2016 at 10:19
  • \$\begingroup\$ @Timelord64 Oops... i know that... actaully i think that was an accident \$\endgroup\$
    – mmoj
    Aug 14, 2016 at 12:35
  • \$\begingroup\$ @mmoj what is the error ? \$\endgroup\$ Aug 15, 2016 at 4:38
  • \$\begingroup\$ @Timelord64 whats wrong in the code, it is untested code giving the accurate concept. \$\endgroup\$ Aug 15, 2016 at 4:39
  • \$\begingroup\$ 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. \$\endgroup\$
    – Gnemlock
    Aug 15, 2016 at 4:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .