I have two different levels and in my second level I want to use facebook to submit a highscore. So I've added the facebook sdk functions (from the fb doc) to my 2nd level unity c# script.
First time loading this level works fine but after I'm done and returning to the first level and loading the second level again it complains because fb.init is already loaded.
My awake() looks like this:
if (!FB.IsLoggedIn) {
FB.Init(SetInit, OnHideUnity);
}
Where do I need to put the FB.Init function so its not getting called again? Both levels are getting loaded again.
As user3811917 has stated, FB init should only be called once. There are many times in Unity development that you only want a function to be called once during the lifecycle of the application, and the most common way of addressing this is to create a separate loading or bootstrapping scene.
Basically, what this scene does is load, then immediately (on start perhaps) transition to the first scene of your game. By adding scripts to this scene (that run on awake), you can call several functions that will only be called once throughout the life of you application. This is useful for setting up third party plugins which need to be initialised only once. You can also place non-destroyable singleton objects in this scene, so that you can create and initialise scene persistent objects, that are only created once.
If you mean levels as the scene in Unity3D, You should call FB.Init once in Unity. Don't try it call method again.
Related
Main Question
I'm trying to load a scene in the editor while in playmode and get the exact behaviour of:
SceneManager.LoadScene(ScenePath, LoadSceneMode.Single);
Except without requiring the scene to be in the build settings.
I would have expected this to work:
EditorSceneManager.LoadSceneInPlayMode(ScenePath, new UnityEngine.SceneManagement.LoadSceneParameters(LoadSceneMode.Single));
However the scene appears to load asynchronously, which I don't want for my particular use case.
Is there a way to acomplish what I'm looking for here?
Additional details (my use case and "why" I want this)
What I'm doing is creating a loader for scene sets that will additively load a bunch of scenes with different constraints on their loading behaviour (such as: load async, always reload when transitioning same->same, and other features). I have this working fine in the editor while I'm not in play mode, I have it working fine in a built player, but the last case is getting this to work while in the editor and in play mode.
The reason I need "single" loading at all is when you transition from one scene set to another and they share no common scenes. Since I can't unload every scene, I'm just skipping the unload step all together and having the first synchronously loading scene load with LoadSceneMode.Single to cause everything else to unload.
On the off chance it's relevant (I really hope not) I'm trying to do this from inside a custom editor's OnInspectorGUI call. I'm triggering it from a button press in there.
Options I'm aware of:
EditorSceneManager.OpenScene with OpenSceneMode.Single
problem: Does not work in play mode
EditorSceneManager.LoadSceneInPlayMode with LoadSceneParameters(LoadSceneMode.Single)
problem: Scene is not loaded synchronously
SceneManager.LoadScene with LoadSceneMode.Single
problem: Scene is required to be in the build settings
EditorSceneManager.LoadScene with LoadSceneMode.Single
problem: Scene is required to be in the build settings
Turns out I had a misunderstanding of what SceneManager.LoadScene(ScenePath, LoadSceneMode.Single); would even do.
A friend had pointed out the docs clearly state the load will happen within 1 frame, not actually at the call-site.
If you're facing a similar issue there are a few options. You could load async and subscribe to that callback to perform activation them. Another option is to subscribe to SceneManager.sceneLoaded and perform activation there.
My question is that, how can I set any scene in unity as first scene (means it will open first when the game starts).
As I started making my game in unity, instead of making main menu I made LEVEL 1, and as a result when the game opens it directly starts the game by showing level 1.
So how can I set my main menu as the first scene
To further upon Retired Ninja's comment, to set a specific default scene when the game opens, you need to set the ordering of the scenes so that the one you want to start with is first, see the picture below:
The main menu screen comes first in the list of scenes, this means it will run on startup.
More details here where it has been answered on the Unity Forums:
https://answers.unity.com/questions/44953/set-first-scene.html
How do i save current scene state and load it later?
I'm making a game where you are building a bridge and then press start to run over it with a car and if you fail you have a restart button that reloads the scene from origin state(without anything built on it). However, i want the player to be able to press an "Edit" button that will go back to the state right before you pressed "start" so you can keep building on your bridge without having to rebuild the whole bridge over and over again. So, how do i do this?
If you don't want to code it yourself, you can use the PlayerPrefs of Unity. If you don't know how to use it look at the documentation (https://docs.unity3d.com/ScriptReference/PlayerPrefs.html) or you can also find tuto on Youtube, even some examples.
In strategy games it's common to have after you click the button to build a building, you are able to "hold" the building on the cursor, so you can place it where you want it to go. To do this, I need to have it initialize, then have it follow the users cursor with raycast.
What I need it to do:
Need to have the building initialize.
Need to have the building follow the cursor using raycast
On click, the building needs to place in a permanent spot, then destroy the old building.
The permanent building gets stored into an empty GameObject using the Transform.SetParent line. Creating a public variable for this above will allow it to be set in the game manager, rather than hard coded into a script.
In general, you will need to create code to update the position of the building to match the position of the cursor. You can get that by getting the RaycastHit point. You can do that by updating the translation of the building on every update to the current hit point.
You will likely need to keep track of the user being in some sort of building placement mode. A simple way to do that is by using a state machine.
Then, when the user clicks (or fires, or whatever you decide the appropriate mechanism is) you use that position to store the permanent position.
It's hard to give more details without know how you are tracking and storing the buildings. I'd assume it's something like an array, in which case, the second step simply involves adding the building to the array.
I have my main menu; when the game start, it takes between 2 and 12 seconds to display the scene. During this time everything is still, and the user may think that the application is hanging or crashed.
I would like to add a simple screen (I can use a canvas and place a simple animation on it with the loading text on it), but I can't get how do you actually tell that the scene loaded.
I did try the delegate "OnLevelWasLoaded", and it is a big lie, because the code running in there is not necessarly happening when everything "pop" on screen and you can start to use the application.
I did try to add a timed function, but it is totally bogus, since I have no way to find out if the scene loaded or not, so on some machines it may be faster, and the game would run behind the loading canvas; while if I do it too short then the scene will not be fully loaded and I would end up showing again the frozen screen.
Beside OnLevelWasLoaded, is there anything else that can be used to intercept when the scene loaded?
You can use:
SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single);
LoadSceneAsync returns AsyncOperationthat you can use in coroutine.
Here you can find more details: Doc
So you can add a loading scene that will start to load proper level asynchronously, this allow you to check when loading is completed.