Instantiated GameObject is Invisible on Android -- But is Visible in Inspector (Should be Visible)
Hey guys. I really need some help. I released my first game on Android about two weeks ago (woohoo), and today I sat down to start working on the second update, which was meant to fix a bug and make the game load a bit faster.
My game is simple. You squash flies that fly around the screen. If you miss, you lose, if you hit it, you get a point and another one instantiates.
So today I got to work, fixed the bug (which was that the game never initialized if the user had disabled the sound in the game, it was a quick fix.
Then I set out to make some animations faster. These animations occur every time the user loads a scene (one animation before load, one after), so making them faster wasted less of the users time. Done.
Then the game broke. At some point hit Build & Run, and it came up on my phone with no fly. I've spent the past 2 hours or so trying to fix it. The problem is that it works perfectly in the editor. Running the game on my computer is no problems at all, but as soon as I build it to my phone, it all goes to hell.
I'm at a bit of a loss for how to even troubleshoot this. I know that the game is technically doing everything except for showing me the actual fly. When I start the game, everything that's supposed to happen does, and if I click the screen, it triggers the failed game that you get when you miss the fly. I've also made use of the debug log and the adb logcat -s Unity command, and using that was able to determine the fly absolutely DOES get instantiated and DOES exist in the game (aside from things in the game happening that the fly controls). So I'm 100% positive it's there. I just don't know where.
This is the code I was using to instantiate it to a random spot on the screen (camera is fixed in this game, doesn't move):
Vector3 screenPosition = Camera.main.ScreenToWorldPoint(new Vector3(Random.Range(0, Screen.width), Random.Range(0, Screen.height), 15));
GameObject newFly = Instantiate(flyPrefab, screenPosition, Quaternion.Euler(0, 0, 0)) as GameObject;
newFly.transform.SetParent(MainMenuGameObject.transform);
newFly.transform.localScale = new Vector3(0.07495514f, 0.07495514f, 0f);
In an effort to get a handle on things, I changed it from a random spot to 0, 0, 0, like so:
Vector3 screenPosition = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, 15));
GameObject newFly = Instantiate(FlyPrefab, screenPosition, Quaternion.Euler(0, 0, 0)) as GameObject;
newFly.transform.SetParent(MainMenuGameObject.transform);
newFly.transform.localScale = new Vector3(0.07495514f, 0.07495514f, 0f);
This, too, works fine on my computer, with the fly starting in the same spot every time, but on my phone it's just invisible still. I'm not getting any errors, and I've really no idea what's wrong.
ONE MORE IMPORTANT THING:
I tried manually dropping a fly prefab into the scene before building it to my phone. I put it in the scene, made it a child of MainMenuGameObject, and set the scale to the same as above. Loaded up the game on my phone, and THAT fly was there. So the prefab is apparently fine. My money is on it somehow being created outside the camera but I've no idea how.
Is there any way to get the scene view in unity hooked up to the instance running on my phone so I can see where the fly is? Any other ideas? I'm at a total loss here. I've done as much troubleshooting as I can think of, but I've run out of ideas to fix this.
Are your flies 3D models ? There's a bug concerning animation and rotation of 3D models in Android builds with 5.3, so if that's the case for your flies check this forum thread on the subject.
If that's not the case, you should log the flies' positions (and maybe scales too) so that you can at least know where they are in the scene
Thanks! I did get it working, see my answer for how. I appreciate your help! And to answer your question, it was a 2D sprite on Android 6.0. Very weird bug.
Answer by krazykhris875 · May 20, 2016 at 08:10 PM
This was a weird bug. I've got a script that modifies the camera some for scaling/positioning on different devices, it seems that what caused this bug must have been that script not having run yet, as what fixed it was ultimately just making the fly instantiate slightly after the start of the scene, instead of on awake. Weird about it working in editor, though. I guess it's just something about the way Android processes. Thanks for your help.
Your answer
Follow this Question
Related Questions
Why is Unity automatically changing the current platform? 0 Answers
Android how to set version to 1.1 0 Answers
Simple project on Android: Builds correctly, runs on device, but it is very buggy. 1 Answer
Windows 10 debug differs from android build 1 Answer
Android Project Split Build Troubles 0 Answers