I have a script called StartGame that is suposed to do different things when different game modes are selected. Unity's API and forums suggests using OnLevelWasLoaded, but it isnt being called. In fact when I hover my mouse over it in Visual Studio it tells me the method is"StartGame.OnLevelWasLoaded". I'm pretty sure that means that It isn't overriding the method. Has anyone else had this problem? Also I'm using Unity 5.3.
Are you sure your GameObject is still existing after loading the new scene?
Usually, all GameObjects are deleted from the scene when loading another scene.
You can prevent this by loading the scene additive (adding the contents of the new scene to the contents of the current one)
SceneManager.LoadScene("your scene name", LoadSceneMode.Additive);
or by preventing deletion for your specific GameObject
GameObject.DontDestroyOnLoad(yourStartGameScriptHolder); // where yourStartGameScriptHolder is the GameObject(!), not the script reference
Related
So, to start, I have this code:
public void switcher(string scene_name)
{
SceneManager.LoadScene(scene_name, LoadSceneMode.Single);
}
That's supposed to load a scene given in the editor, and I have 2 buttons, each supposed to load a specific scene and 2 gameobjects (one for scene) that have the code written before.
Object with script
Button that calls object
Now, the thing is, despite having different scene names, both buttons load the same scene, and when I do some changes (mainly deleting and re-adding components) then both buttons load another scene, but never one scene each.
Why is this happening?
First of all to load your scene you must add it to the build settings.Go to File>Build settings and then drag your scene into the Scenes in build.Sencond thing you must add this line of code to set you scene to be the active one
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + *Add you scene number here*);
I'm still a bit lost on this. Basically I want to have a persistent gameobject throughout multiple scenes. This gameobject will represent the players avatar. It is displayed using a prefab.
I tried adding the singleton script to my login scene controller. I have a public GameObject class parameter but it isn't showing up in the inspector so I can drop the prefab in. Maybe I'm getting this wrong.
Also, let's say I'm testing a scene. Since the singleton is initialized in the login scene, How would I get this global gameobject prefab into that new scene I'm testing considering I won't even be loading the login scene during testing?
I think I'm just super confused. All I want is to be able to use 1 script which represents the players "avatar", including the prefab used to display it, and have it persist throughout the entire game. Is this possible? Also, how do I get it when I'm testing scenes and haven't called the scene containing the singleton?
Thanks for anyone who can help me.
Basically I want to have a persistent gameobject throughout multiple scenes. This gameobject will represent the players avatar.
Use DontDestroyOnLoad on the object that should persist throughout multiple scenes.
Take caution if you are loading back the same scene that set the Object to DontDestroyOnLoad, as it will create another same object that has a DontDestroyOnLoad. This post talks about it.
Edit (Thanks to yes), check the comments in this answer.
(There are also a few more options that you have depending on your needs, Hugo's answer from this post has it.)
I tried adding the singleton script to my login scene controller. I have a public GameObject class parameter but it isn't showing up in the inspector so I can drop the prefab in. Maybe I'm getting this wrong.
Show some codes? It is hard to exactly tell what is wrong.
It might be the fact that you forgot to drag in your script to a game-object, or the class is static, etc.
I made a scene that represents the main menu of a game. In this menu there is an option for single player, and, when I press it, it opens a new "level 1" scene.
Also, there is a pause menu that allows the player to quit the game and get back to the main menu.
However, after it backs to the main menu, when I try to get into the level 1 scene using SceneManager.LoadScene("level 1"), it makes a lot of problems. It means that a lot of game objects don't act like they should.
I dont know why it happened, and I would appreciate if someone can help me solve it. Thank you.
You can use SceneManager.LoadScene("level 1", LoadSceneMode.Single); to clear the old scene and only load the new one in, which will let you get a clear scene and shouldn't have any problems.
If you have scripts that need to last into the new scene, you can use DontDestroyOnLoad(this); in your scripts and they will move into the new scene. The problem is you have to make sure your scripts that aren't being destroyed don't try and access destroyed objects. From your comment it looks like you have a player movement controller that isn't being destroyed, or it's being created in the menu scene and it doesn't have it's references updated correctly when the scene is reloaded. You can also use Destroy(this); in your scripts that are saved through scene changes to get rid of them when they're done running.
I am a beginner with Unity3D.
Until now I have learned a few things.
For example, create a simple 2D menu, switch from one scene to another with an appropriate C# script and associate the scripts with the objects.
The problem I pose here is about the behavior (strange to me) of the white panel behind the menu when I change the scene.
I have two scenes, the first which is the menu and a second one is the actual game.
I used UI elements to create the first one, while I used 3D objects to make the second one.
So, what happens?
It happens that when you step into the second scene by clicking on the green button the background becomes brown. If, on the other hand, I run the second scene individually without going through the first one (launching the scene directly), the background remains white.
See this short video for further details.
Why?
Listed below are some images, hoping they can help me understand my problem.
The first scene (dev mode):
The first scene (running):
The second scene (dev mode):
The second scene (running directly):
The second scene (running after clicking the green button "Comincia"):
The script I use to change the scene (linked to the green button):
using UnityEngine.SceneManagement;
using UnityEngine;
public class GameScene : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void PlayNow () {
SceneManager.LoadScene("Game", LoadSceneMode.Single);
}
}
Update: I have updated the information on the application. I added a new screenshot where you can see the camera settings. Meanwhile, I changed some things, but the problem still remains. I also noticed that this problem only happens when I switch to the Game scene, when I switch to other scenes instead it does not.
Added a new screenshot showing the inspector of the Main Camera object:
I was able to understand where the problem was: I made an improper use of the Plane object (highlighted in the screenshot). By removing it, now it shows correctly the solid color white background as set on the Main Camera.
Example
In Unity5, assuming that a GameObject with name "SomeObject" was stored as a prefab at Assets/Resources/SomeObject.prefab, I know that I can create an instance of the prefab as follows:
GameObject prefab = Resources.Load<GameObject>("SomeObject");
GameObject instance = GameObject.Instantiate(prefab);
Once executed, the instance GameObject will be a copy of the original "SomeObject" prefab, and will have been placed in the current active scene with the name "SomeObject(Clone)".
As far as I understand, the GameObject prefab represents the actual prefab asset, and any changes made to it (setting name, adding components, etc) are written to the original prefab asset, and these changes persist even after exiting editor play mode.
Questions
Since all GameObjects are normally stored in a scene, and the scene property on GameObject prefab in the above example seems to be Unloaded/Invalid/Unnamed, where exactly should I consider this special GameObject to be? I seem to be able to do everything with it that I can do with normal GameObjects, short of being visible in the hierarchy/scene view.
Is it effectively in some kind of limbo state, or a special PseudoScene? It seems to persist through LoadSceneMode.Single scene changes, but is not like the special DontDestroyOnLoad scene that Objects are moved to when passed into GameObject.DontDestroyOnLoad(...).
Are there any other noteworthy differences between prefab and instance in the above example, other than lifetime changes, the invalid scene and being different objects from one another (Having different GetInstanceID(), etc)?
Since calling GameObject.Instantiate(...) on prefabyields a GameObject that is in a valid scene, is there any way to manually create GameObjects that are in a similar 'no scene' state? I am aware that Unity intends for all GameObjects to be in scenes and so this is purely an academic question.
I have tried looking through the documentation of the functions involved, but none go into the technical details of what specifically is happening when you call Resources.Load on a prefab resource.
1.Where exactly should I consider this special GameObject to be?
It's in a separate file and not referenced in the scene. Once Resources.Load<GameObject>("SomeObject"); is called, it is loaded into the memory waiting to be used when GameObject.Instantiate is called.
If it is declared as public GameObject prefab; and assigned from the Editor instead of GameObject prefab Resources.Load<GameObject>("SomeObject");, it will be loaded into the memory automatically when the scene loads.
2.Are there any other noteworthy differences between prefab and object?
Yes.
Prefabs cannot be destroyed with the Destroy or DestroyObject function. It has to be destroyed with the DestroyImmediate function by passing true to its second arguement: DestroyImmediate(prefab, true);
3.Is there any way to manually create GameObjects that are in a similar 'no scene' state?
No, there is no way to manually create GameObjects that are in a similar 'no scene' state.
Although you can fake it.
The closest thing to that is to make the GameObject invisible in the Editor's Hierarchy and Inspector tabs by modifying the GameObjects's hideFlags variable then deactivate that GameObject.
GameObject obj = new GameObject("SomeObject");
obj.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;
After that, deactivate the GameObject:
obj.SetActive(false);
Now, the GameObject is invisible in the Editor and it is also invisible in the Scene and Game View.
There is really no big difference between prefabs and a typical GameObject. Prefab is just a container that holds the GameObject so that it can be re-used and shared between scenes. Modifying one prefab will update any instance of it.
Imagine when you have hundreds of GameObjects of the-same type in the scene and need to modify all of them. With prefab, you only need to modify one of them or just the prefab then click Apply to update other instances. This is the only thing you should think of prefabs.