- Home /
The question is answered, right answer was accepted
Can't find game object / script after scene re-enter
Hi. I'm developing a iOS game with Unity, and am currently trying to implement Unity's IAP. I have a basic in game shop that's inside another scene from the actual game scene. When I enter the shop scene for the first time, everything works marvelous. I can purchase objects, and while the transaction is ongoing, I hide my UI, and when it's complete (or aborted), i show my UI again. This is the intended behavior.
If I now enter my main scene, the game itself, and then re-enter the shop scene and try purchasing another product, the UI hides, iAP is popping up as expected, but the UI stays hidden. This is my problem.
While debugging on iOS I'm getting a NullReference telling me it can't find the UI.
I'm using SceneManager.LoadScene
to switch between scenes. The Script that calls SceneManager.LoadScene
is attached to a Game Manager object which gets recreated each time the user enters the scene(s)
Any help is much appreciated!
This is pretty impossible to answer without some actual code. You mention debugging on iOS- you mean this doesn't happen when debugging in the editor?
@tanoshimi Hi, thanks for your reply. I'm not home at the moment but as soon as I get home I'll update this page with some acctual code. It's nothing special though, all I do is assigning the gameobjects inside the editor. Works the firdt time you enter the scene, the next time it can't find the objects i believe. I've tried GameObject.Find
aswell as gameObject.GetComponent
, all functions yields the same result.
As to your question, no. Doesn't work in the editor either. Does unity keep some level cache or something? And then assigning invalid pointers somehow?
No. The fact that it works/breaks on reloading scenes suggests it's some combination of DontDestroyOnLoad objects that have dependencies on other objects - are you using that pattern?
Answer by tuncOfGrayLake · May 03, 2016 at 07:49 PM
1 - Just a guess but perhaps something gets lost in between loading the scenes? Maybe you need to ask some object or scripts to DontDestroyOnLoad (More info here).
2 - Perhaps the names of the GameObjects are different the second time you enter the scene OR they're called before the objects are instantiated again in the new scene OR the objects are disabled when you're calling the GameObject.Find?
These are the usual suspects I had in mind. Best of luck!
I've found the solution!
I tried @tuncturel 's guidance and made my purchaser stick throughout the games lifetime, and now it works!
For the curious, this was my solution:
private static Purchaser Instance = null
void Awake()
{
if (Instance == null) {
Instance = this;
DontDestroyOnLoad (gameObject);
} else {
DestroyImmediate (gameObject);
}
}
void OnLevelWasLoaded(int level)
{
if (level != 1)
return;
SelectorGO = GameObject.Find ("SkinSelector");
selector = SelectorGO.GetComponent<SkinSelector> ();
}
Follow this Question
Related Questions
Scene loading delay after upgrade 4.6 to 5. 0 Answers
Unity Scene not loading. 1 Answer
Scene Loading Issue. 0 Answers
Does Active/Inactive and Enabled/Disabled save memory? 1 Answer
How do I return scene using bulid index scenemanager in c# 3 Answers