Object cant be found in new scene
Sorry I'll try to explain this as best I can but I am a Unity and C# n00b.
Here are my Build Settings:
MENU = 0
GAME = 1
LEADERBORAD = 2
PLAYER NAME GENERATOR = 3
Now I'm in GAME scene and when the player loses a"CheckScore" from a Class from Leaderboard scripts is called to see if the score the player got belongs in the leaderboard.addScore = GameObject.Find("PassScoreObject").GetComponent(); addScore.CheckScore(300);
Now in "LeaderboardManager.cs" >> which is in Leaderboard scene 2 but is applied to the "PassScoreObject" in Game scene 1<< this is the CheckScore function:
public void CheckScore( int score)
{
int i = 0;
GetScores(); //Generates a list from the DB
HighScore tmpObject;
SceneManager.LoadScene("Leaderboard");
for ( i=0; i < highScores.Count; i++){
tmpObject = highScores[i];
if ((tmpObject.Score <= score) || (highScores.Count < 10))
{
callToGetPlayerName = GameObject.Find("GetName").GetComponent<ScrollViewAdapter>();
callToGetPlayerName.GenNames(score);
}
}
//return 0;
}
I'm asking it to find the object "GetName" in the Leaderboard scene with the script "ScrollViewAdapter" attached to it, which it should then be able to access the "GenNames" class, however, this is the error I'm getting.
'NullReferenceException: Object reference not set to an instance of an object LeaderboardManager.CheckScore (System.Int32 score) (at Assets/Leaderboard/LeaderboardManager.cs:163)'
The object "GetName" is definitly in the Leaderboard scene and it has the script "ScrollViewAdapter" attached.
I've tried to debug the SceneManager:
Debug.Log("This is the leaderboard " + SceneManager.GetActiveScene().buildIndex);
This comes back as the value 1, which is the Game scene, so I'm assuming thats why it cant find the "GetName" object in the Leaderboard scene because it's still looking in the Game scene for it :/
I have also declared 'callToGetPlayerName' at the start of my LeaderboardManager.cs script
private ScrollViewAdapter callToGetPlayerName;
Your answer
![](https://koobas.hobune.stream/wayback/20220612212922im_/https://answers.unity.com/themes/thub/images/avi.jpg)