Im new to C#/Unity (be gentle) - I'm trying to create my own script that adds a public variable to the Inspector, doing this allows me to have one script be applicable to multiple GameObjects (or so I thought).
The script is pretty simple.
It works on the PC, but it doesn't work on Android.
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.EventSystems;
public class BtnLoadScene : MonoBehaviour {
public Object SceneObj;
private string SceneName;
public void LoadLevel()
{
SceneName = SceneObj.name;
Debug.Log (SceneName);
SceneManager.LoadScene(SceneName);
}
}
I'm attaching this script as a component to ALL of my buttons (across the entire app) that drive scene loading. Again, the value here is that I can then drag my "scene" from assets folder over into the inspector/component (and thus I won't have to explicitly manage "ints" or "strings" - it'll be all done through object references).
See for example:
Again, this works on the PC UPDATE: As per recommendation in a comment, I tried this in the standalone player and it failed. This ONLY works in the Unity Editor. When I build for Android (yes it compiles and I can install the APK) it doesn't work - the first scene loads, the button is responsive to my touch and I see it "depress", but ADB throws:
12-23 15:37:27.028 14816 14847 I Unity : NullReferenceException: Object reference not set to an instance of an object
12-23 15:37:27.028 14816 14847 I Unity : at BtnLoadScene.LoadLevel () [0x00000] in <filename unknown>:0
12-23 15:37:27.028 14816 14847 I Unity : at UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) [0x00000] in <filename unknown>:0
12-23 15:37:27.028 14816 14847 I Unity : at UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) [0x00000] in <filename unknown>:0
12-23 15:37:27.028 14816 14847 I Unity : at UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) [0x00000] in <filename unknown>:0
12-23 15:37:27.028 14816 14847 I Unity : at UnityEngine.Events.UnityEvent.Invoke () [0x00000] in <filename unknown>:0
12-23 15:37:27.028 14816 14847 I Unity : at UnityEngine.UI.Button.Press () [0x00000] in <filename unknown>:0
12-23 15:37:27.028 14816 14847 I Unity : at UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) [0x00000] in <filename unknown>:0
12-23 15:37:27.028 14816 14847 I Unity : at UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) [0x00000] in <filename unknown>:0
12-23 15:37:27.028 14816 14847 I Unity : at UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target
12-23 15:37:27.036 14816 14847 I Unity : NullReferenceException: Object reference not set to an instance of an object
12-23 15:37:27.036 14816 14847 I Unity : at BtnLoadScene.LoadLevel () [0x00000] in <filename unknown>:0
12-23 15:37:27.036 14816 14847 I Unity : at UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) [0x00000] in <filename unknown>:0
12-23 15:37:27.036 14816 14847 I Unity : at UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) [0x00000] in <filename unknown>:0
12-23 15:37:27.036 14816 14847 I Unity : at UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) [0x00000] in <filename unknown>:0
12-23 15:37:27.036 14816 14847 I Unity : at UnityEngine.Events.UnityEvent`1[T0].Invoke (.T0 arg0) [0x00000] in <filename unknown>:0
12-23 15:37:27.036 14816 14847 I Unity : at UnityEngine.EventSystems.EventTrigger.Execute (EventTriggerType id, UnityEngine.EventSystems.BaseEventData eventData) [0x00000] in <filename unknown>:0
12-23 15:37:27.036 14816 14847 I Unity : at UnityEngine.EventSystems.EventTrigger.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) [0x00000] in <filename unknown>:0
12-23 15:37:27.036 14816 14847 I Unity : at UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) [0x00000] in <filename
If I put a hardcoded value (string or scene int) it works fine in Android and PC. But, for some reason that I can't see, it appears that the Scene object that I've referenced in the Inspector, when I build for Android, isn't capturing the String name and passing it to LoadScene() like I thought.
Can anyone advise on what I'm doing wrong? Is this not possible or a dumb idea?
Thank you in advance.
Go to the Unity Editor. From the menu, choose 'File', 'Build Settings…' In the Build Settings dialog, under 'Platform', select 'Android' as the target and click 'Switch Platform'.
Scenes are saved as assets, into your project's Assets folder. Therefore they appear in the Project Window, just like any other asset.
I'm afraid that UnityEngine.SceneAsset
can be only used in Editor and it will end up being null in any build.
However, Unity3D needs only a string or index from BuildSettings
list so it's a no-brainer: just pass scene name in inspector. It would be a constant throughout your development either way and would behave the same if you change scene name as if you would put actual SceneAsset
object in there (in both cases would fail on rename).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With