Cannot Change to a specific scene in Unity after building it to mobile device - unity3d

Currently I'm using Application.load() to change scenes and it works perfectly in Unity. When I built it to a mobile platform and tested it, only in that one scene I can't change to a specific scene.
For example, gameplayScene, levelSelectionScene, mainMenu. I'm able to change scene from mainMenu to levelSelectionScene and then to gameplayScene. For unknown reason, I'm unable to go back to levelSelectionScene from gameplayScene while I can change scene to mainMenu from gameplayScene.
Below is the sample code from button that goes to levelSelectionScene from gameplayScene
private void OnClick ()
{
Debug.Log ("clicked");
if (PlayerPrefs.GetInt("SanguineDifficultyAchieved") == 1)
{
Debug.Log("Entering Difficulty");
m_Owner.SetActive ();
}
else
{
Debug.Log("Exiting");
State.Current = State.NotInGame;
Application.LoadLevel(scene.name);
}
m_Owner.close ();
I don't understand why it works on Unity debugger but then it doesn't work on mobile platforms.
Update 1
I tried to use numbers instead of strings it worked well. But I still don't understand the reason why.

Finally got an answer. It seems that the scenes collided with each other because I use scenes from Asset Bundle and the added scenes from build settings. That is why the when i use Application.Load(int number) works because i only access the scene from build settings.

Related

Can't enable AudioSource after Unity crashed

Yesterday everything was working perfectly. The build on my android still does. But last night when I was trying to add some new code to read from a json file, Unity crashed. I had to force quit, and when it came back I couldn't see anything that changed however none of my sound effects would play. They all gave me the "Can not play disabled audio source" error. I thought something must have changed that I can't pinpoint, but no matter what I tried I couldn't get them to play again.
So I started a whole new project with just 1 audio source prefab, 1 game object in the hierarchy, and 1 simple play script to test. The script is attached to the game object and I passed the audio source prefab to the inspector variable. And it still gives me the error. When I Debug.Log the isActiveAndEnabled property is false. Enabled was false too so I tried to manually force it to true, but no change. I feel like something really messed up in Unity during the crash and I don't know how to fix it. I see other questions with this error but they are all either missing a basic checkbox or destroying the object, neither of which I'm doing. I don't know how to debug further than this. The audio clip plays fine in the preview. I appreciate any guidance. This is all my code is right now:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class testSound : MonoBehaviour
{
public AudioSource abc;
void Start()
{
abc.enabled = true;
Debug.Log("abc is active/enabled: " + abc.isActiveAndEnabled);
Debug.Log("abc enabled: " + abc.enabled);
Debug.Log("abc tag: " + abc.tag);
abc.Play();
}
}
I don't see any issues in your code as you only need the component and .Play() function. Logically, the issue should be somewhere so here are some tips that may help you:
Check during play time if the GameObject that have the AudioSource component is still active/not destroyed, since your "isActiveAndEnabled()" returns false, the problem is probably there.
Try to put the AudioSource component in the same GameObject as the script, this way you ensure that both are either enabled or disabled.
Uncheck the "Play on Awake" and try again, since your code is already doing the work of .Play()
Also, I don't know if you tried but the "Play on Awake" option should work 100% of the time without any code. So normaly you should hear the sound at the beginning of your gmae or when the object is enabled.
Hopefully it will guide you the solution.

Unity3d ARKit not reset

I am using vuforia & ARKit sdk in my application.
I have two buttons ( Vuforia & ARKit buttons)
My app work flow:
Open the app
Clicked on “vuforia AR” button (1st time)
If marker found Vuforia AR works.
i am switch to “ARKit” 1st time, it will be working fine.
If we press back button, it will redirect to MainScreen
Again Click on “vuforia AR” button (2nd time)
Vuforia working good.
If we switch to “ARKit” , ARKit Camera is not resetting .
Here arkit camera is freezing.
Note: both SDK are using single scene and I tried two different scenes also. My issue is not resolved.
Don't know if you solved your problem.
If not, did you tried to reset your ARKit scene ?
Here's a sample code I founded in another post. Working fine for me to reset my ARKit Session.
public void ResetScene()
{
ARKitWorldTrackingSessionConfiguration sessionConfig = new ARKitWorldTrackingSessionConfiguration(UnityARAlignment.UnityARAlignmentGravity, UnityARPlaneDetection.Horizontal);
UnityARSessionNativeInterface.GetARSessionNativeInterface().RunWithConfigAndOptions(sessionConfig, UnityARSessionRunOption.ARSessionRunOptionRemoveExistingAnchors | UnityARSessionRunOption.ARSessionRunOptionResetTracking);
}
Don't forget to change sessionConfig parameters with yours ! ;)

Failed to pass eligibility in the Google Daydream Appstore for my hybrid Daydream + Cardboard app using Unity VR

I am getting the issue: The app does not use the Daydream controller properly
The app allows users to use head gaze to position the cursor and interact with menu UI's "Play" button.
Here is my class that sets the GvrPointerInputModule.Pointer to either use the GVRLaserPointer (for daydream) or the GvrReticlePointer (for cardboard):
public class InputModuleSelector : MonoBehaviour {
bool DaydreamControllerConntected = false;
public GvrBasePointer DayDreamController;
public GvrBasePointer CardboardController;
private void Start() {
refreshControllers();
}
public static bool IsDayDreamMode() {
return VRSettings.loadedDeviceName != "cardboard";
}
private void refreshControllers() {
DaydreamControllerConntected = IsDayDreamMode();
DayDreamController.gameObject.SetActive(DaydreamControllerConntected);
CardboardController.gameObject.SetActive(!DaydreamControllerConntected);
GvrPointerInputModule.Pointer = DaydreamControllerConntected ? DayDreamController : CardboardController;
}
}
If we use the daydream, we will use the laser, otherwise we will use the reticle.
How can I get my app to pass? Are we allowed to submit an app that can be used for both daydream and cardboard? Let me know if you need more information.
I am not sure if I should include a link... but my app is active on the playstore: https://play.google.com/store/apps/details?id=com.fungamefuntime.warehouse
It appears as though you let the cursor be controlled with head gaze. Head gaze is allowed as a controller substitute in all elements of gameplay, except when menus appear.
I was able to figure and correct the issue with the help from Google Support. The main Camera rotates with the player's gaze direction. This means that the GvrControllerPointer depends on the main camera. This violated the requirements because you moved the laser when you also moved your head. Hierarchy View:
Player
Main Camera
GvrReticlePointer
GvrControllerPointer
When I switched to this structure, the problem was solved. The GvrControllerPointer is now independent of the Main Camera and the player's gaze.
Player
GvrControllerPointer
Main Camera
GvrReticlePointer

Manually programming the splash screen to appear at arbitrary times

In our app, the splash screen appears when we start the app (or build to Android device) as the app is loading, which is done through the Unity's Edit -> Project Settings -> Player but we now have a feature that sometimes in the middle of the app also re-starts the app, so we would like to code this behavior, so that we can show a different splash screen if the app re-starts mid-usage.
We cannot seem to be able to figure out how to do this programmatically, or where exactly in the app code, so we would appreictae any help.
What we do know (and have already implemented) is through:
PlayerPrefs.SetString("LastShownComponent", menuId);
PlayerPrefs.Save();
which remembers if this is the first time the app is starting (original splash image) or whether the user is mid-usage, but how do we specify another image to be loaded as splash screen when the app is reloading mid-usage?
EDIT: more details...
Previously, we only had the following code:
if (_callbackUri == null)
{
SceneManager.LoadScene("ReloadScene");
}
Now, we force the app to re-start mid-usage (for a specific reason) by:
if (_callbackUri == null)
{
PlayerPrefs.SetInt("Restarting", 1);
PlayerPrefs.Save();
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidPlugin.Restart();
#else
SceneManager.LoadScene("ReloadScene");
#endif
}
However, when it restarts, obviously it re-loads the same splash screen image that is in the player settings.
We probably need to add code just below AndroidPlugin.Restart(); to load a new splash screen image, but how do we do that? Do we need a new scene for that?
Per the comments:
Quickest way to test this is to create a blank scene, add a gameobject to that scene name it something like SplashLoader and give it a script.
the only thing you need in that script is the start method,
void Start()
{
// Default to 0 incase this value isn't stored
int reloaded = PlayerPrefs.GetInt("Restarting", 0);
// Reset to 0 so if the game is closed without restarting it will display the correct scene
PlayerPrefs.SetInt("Restarting", 0);
PlayerPrefs.Save();
if(reloaded == 1)
{
SceneManager.LoadScene("ReloadScene");
}
else
{
SceneManager.LoadScene("SplashScene");
}
}
From here you would create 2 scenes, one for your normal SplashScene and 1 for your ReloadScene. In those scene you can create a canvas object and add an image to it then change the image depending which scene it is.
Another Option if you want to use a single scene and keep the same animation say you have an effect where your logo fades in, you can do this:
Create a scene for your splash screen, make that your 0 index scene, add a canvas to it and an image, then use an animator to get your splash screen effect going.
Add a script to your image object, to change the picture depending on what the preference stores, you can use almost the same code as the start method above, instead of calling the SceneManager to load the scene you would just update the image.
Short answer: you can't.
The splash screen (and everything associated with it) can only be changed/modified inside the editor (via Inspector or via code using the UnityEditor.PlayerSettings.SplashScreen class).
To add insult to injury, on Android you MUST have a splash screen: if you try to disable it via the Player Settings, the Android app won't work.
So, the only solution you have is to use a blank splash screen, and then two different starting scenes (with different backgrounds) which are loaded, mutually exclusively, if you are launching or restarting the app (by using a field in PlayerPrefs as you correctly thought).
EDIT
Actually, you don't even need multiple scenes to accomplish what you want to do.
Create just one scene (let's call it SplashScene), with just the Main Camera and a Canvas with an Image.
Attach this script to the Main Camera:
using UnityEngine;
using UnityEngine.UI;
using System;
public class NewBehaviourScript : MonoBehaviour {
[SerializeField]
Image backgroundImage;
[SerializeField]
Sprite launchBackground, restartBackground;
private void Awake() {
if (!PlayerPrefs.HasKey("Background Splash Screen"))
PlayerPrefs.SetInt("Background Splash Screen", 0);
int background = PlayerPrefs.GetInt("Background Splash Screen");
switch (background) {
case 0:
backgroundImage.sprite = launchBackground;
break;
case 1:
backgroundImage.sprite = restartBackground;
break;
default:
throw new Exception("Invalid Background Splash Screen PlayerPref value!");
break;
}
}
}
Of course SplashScene should be at index 0 in the standalone scenes list.
The only other thing you need to do is to add
PlayerPrefs.SetInt("Background Splash Screen", 0);
before any Application.Quit(); method you call in your project and
PlayerPrefs.SetInt("Background Splash Screen", 1);
before any AndroidPlugin.Restart(); method in your code.
Don't forget to assign the sprites and the image reference to the variables of the script via Inspector ofc (you can get them via GetComponent and asset loading if you prefer, it's up to you).
That's pretty much it. :)

Unity3d EasyAR Target loads at the start of the camera before find the trarget

I am tring to build an AR android app with unity3d last version and EASYAR SDK.
I have create the target and i am able to load my image or video at it's chile object but the child objct (cube or plane) load imediately when th camera opens.
i am looking some help on how i can apear the image or start the video when the camera find the target and not when the app starts.
thnns a lot in advanced guys for the help!!!
You can simply disable them in the beginning and modify the OnTargetFound event of the ImageTargetBehaviour script to enable them. Something like this:
public GameObject myARObject;
void OnTargetFound(TargetAbstractBehaviour behaviour)
{
Debug.Log("Found: " + Target.Id);
myARObject.SetActive (true);
}
Hope this helps.