Script not executing in Android build but working in Editor
Hi,
Issue: My game is working correctly in Unity Editor but when I build for mobile, some scripts don't run and functions aren't called.
What I've tried: I have tried changing my code multiple times, shifting logic to and from Awake and Start and trying to force script execution order. I also tried calling the CheckCompletionQuestLine() method with a delay using a coroutine and this still didn't work. The only thing it should need to run is the reference to my SaveManager (which it gets before calling the function) and those GO and component references at top (which are all assigned manually in the inspector).
Code: Here is my code example for the script that isn't working on start on Android. This code sits on a GameObject in my game scene.
SaveManager saveManager;
public GameObject courtyardConfines;
public GameObject castleGardenConfines;
public BoxCollider2D marketToCastleTransfer;
public GameObject princessQuestBubble;
public GameObject executionerQuestBubble;
private void Start()
{
saveManager = GameObject.FindGameObjectWithTag("SaveManager").GetComponent<SaveManager>();
CheckCompletionQuestLine();
}
public void CheckCompletionQuestLine()
{
if (saveManager.myStats.questProgress.Count == 0)
{
UpdateQuestBubbles(executionerQuestBubble, true);
}
else if (saveManager.myStats.questProgress.Contains("Quest1Complete"))
{
Quest1CompleteToggles();
}
else if (saveManager.myStats.questProgress.Contains("Quest1Part1"))
{
Quest1Part1Toggles();
}
}
private void Quest1Part1Toggles()
{
courtyardConfines.SetActive(true);
castleGardenConfines.SetActive(true);
marketToCastleTransfer.enabled = true;
UpdateQuestBubbles(executionerQuestBubble, false);
UpdateQuestBubbles(princessQuestBubble, true);
}
private void Quest1CompleteToggles()
{
castleGardenConfines.SetActive(true);
courtyardConfines.SetActive(false);
UpdateQuestBubbles(princessQuestBubble, true);
}
private void UpdateQuestBubbles(GameObject charBubble, bool truefalse) => charBubble.SetActive(truefalse);
Do you have any suggestions to make this behave on my Android build as it does in Editor? Thank you for reading.
Answer by misamiso · Oct 15, 2019 at 11:15 PM
Hi all,
I have managed to solve it. My list was null so .Count would not work and would throw an error in Android build. I changed my code to use the null conditional operator to check if null, and if no, to check if it is empty of elements like this: if (list?.Any() != true) instead of if (list.Count == 0) to check whether to trigger certain actions. https://stackoverflow.com/questions/24390005/checking-for-empty-or-null-liststring/43142462
No more null refs on monitor logcat and my game works correctly on mobile build now.
I had the same Problem and pretty much the same Reason. For Windows I check in Awake() for the heighest possible Resolution heighestResolution = Screen.resolutions[Screen.resolutions.Length - 1];
. This works in Editor and Windows, but not on Android, which immediately deactivates the whole Script. Now I make sure that this Code only gets executed, when I am not on Android: if!(Application.platform == RuntimePlatform.Android)) { //Exclusive_Code_Here }
Thank you for co$$anonymous$$g back and sharing your Solution. It saved me a lot of time!
Your answer
Follow this Question
Related Questions
Android apk lags on all devices exept of mine 0 Answers
Unity 2017.2.0b9 ARCore build&run with Pixel XL (Android 8.0) Failed 0 Answers
mobile device restarts when game is uninstalled 0 Answers
Performance much worse on LWRP than on BuiltIn Pipeline 0 Answers
does direct SQL connection with Unity work in Android? 0 Answers