What I'm trying to do inside this script in Unity is to load a different scene, and find the canvas from that scene and enable it. I already know how to enable the canvas from the same scene but the issue is how can i find and enabled a canvas from another scene.
I tried SceneManager.LoadScene(scenename); to change scene so what should I add or modify? Can someone help me?
Save the canvas you want to switch to as a 'prefab' in your assets folder.
If you are loading a new scene without carrying over data from the last scene then just set up the canvas in that scene using the editor.
If you are carrying data over, or perhaps a player gameobject, then you can instantiate the canvas prefab you need. This would create it in the new scene and you can have a reference to it from wherever you chose to instatiate the prefab.
Alternatively you can have multiple canvases on the same gameobject and switch them on or off as you need them. Very useful for menus.
Hope that helps.
There're many solutions:
Using MonoBehavior's DontDestroyOnLoad(transform.gameObject) for your canvas you need to be on the next scene; You can find a lot examples in Google. Here's the link : https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
Saving somewhere your current scene's gameobjects(canvas) and loading it again and recreate on the next scene as serialization etc;
Same as #2, but save required data in local variables.
There are two ways I understand your question.
You either want to copy a game object with all it's parameters as defined in the editor into another scene.
The best way to do that is obviously to use prefabs. (where to start?)
If you want to copy a game object and all its parameters after they have been modified at runtime, then you could use DontDestroyOnLoad() but that would mean the source scene has to be loaded at least once before the destination scene.
Related
Looking at the Profiler data, it seems that WorldSpace canvases (I got a bunch of those) who should support click events (they got buttons), are making a lot of calls to FindMainCamera through the EventSystem.Update() function. I assume this is because all of these canvases are Prefabs and so it isn't trivial for them to get a reference to the active camera from within the editor in prefab mode.
In this project I'm not using Dependency Injection, and I only got one camera in the game, so I was thinking maybe I should just create a SingletonCamera component and then have the Prefab canvas reference it.
I do wonder though if there's a better solution, as this seems like a pretty common scenario that a WorldSpace canvas would be inside a prefab, and have no easy access to a camera.
Thanks!
You can use the FindObjectOfType function to get a reference to the active camera at runtime.
This function searches the scene for a component of the specified type and returns the first one it finds.
So, you could add a script to your WorldSpace canvases that uses FindObjectOfType to get a reference to the active camera when the canvas is enabled or when the canvas needs to interact with the camera.
I am trying to create a tower defense game and I made a menu scene and a level01 scene.
Now when I create some objects in the menu scene, and go to the level01 scene, I lose all my objects in my menu scene. I don't know what is going on. I have tried to create a new menu scene and duplicating my level01 scene, but it doesn't work.
You can use DontDestroyOnLoad or you can add multiple scenes and never unload the menu scene.
SceneManager.LoadScene("OtherSceneName", LoadSceneMode.Additive);
The use of a scene, as its name implies, is to prescribe a different scene and arrangement. If you can design the gameplay inside a scene and deactivate them beforehand instead of adding objects. This will make the code more consistent and will prevent future problems.
You can mark objects to keep them between loading scenes by DontDestroyOnLoad(myObject). Here is the documentation with sample code:
Object.DontDestroyOnLoad
I am new to unity and working on a projects.
I want to work with multiple scenes.
some of my scenes are like option menu in the game.
from my main screen I want to open an options scene and when I am done I want to move back to my main scene and when I am back I want to keep the things done in main scene before the options scene opens
I can change scenes with SceneManager but it loads the screen as new as if I did nothing there
is it possible to switch between loaded scenes without loading again? I think that if it is; I can continue from the progress in the main scene
if it is not how can I continue my progress (do I have to keep all the data and when the scene starts load back from that data? )
The SceneManager class provides lots of useful ways to manage your scenes. You can find documentation here.
Using multiple scenes to separate your logic is a great approach, and using the LoadSceneMode.Additive option when loading a new scene lets you load one scene alongside another.
To achieve what you want, you'd roughly need to do the following:
Load your main menu scene.
Load your options scene additively with a call like SceneManager.LoadScene("path/to/options/scene.unity", LoadSceneMode.additive).
Pass input control to your other scene.
Unload the options scene when you are finished with it.
The main menu scene will have been loaded for the entire time, and you won't have to "remember" any values or use DontDestroyOnLoad.
An alternative option is to house all of your menu functionality in a single scene, and switch between multiple canvases. You can find information about that here.
you can create a class with values that you want to keep and put it on a gameobject
and use DontDestroyOnLoad(this.gameObject); so the object won't be removed when you load a new scene
I have a Player gameObject that has a script on it which keeps some variables inside it. I created a scene which acts as a main menu and has "Shop" part in it, which has upgrades in it, basically I need to access Player gameObject's script from a different scene so I can modify variables from main menu scene. How can I do that ?
check this out:
https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
once your player is loaded this will keep him in every scene until you choose to release hiim, meaning when you load your menu scene he will be there to access.
now since we know hes there, we can:
(note that player is your script with the variables not your actual player)
ScriptName player = GameObject.FindObjectWithTag("yourtag").GetComponent<ScriptName>();
Be default the components are instances of a class. If there isn't a GameObject with that component in the currently loaded scenes, there is nothing to access.
If I recall you can now load more than one scene at a time.
If there is a GameObject with that script currently loaded you can use:
https://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html
or even better
use singleton design pattern, in case you have only 1 player at a time.
I guess you want to save these upgrades for the next run of the game, so you can use PlayerPrefs
https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
If you want to save more than 50 values of data I would suggest using a file based DB like SQLite, it takes little time to setup and works very fast. And it is a lot more readable than using PlayerPrefs.
How about static class?
You can keep values there and access it from any place you want :D
I've been developing a board-style game in Unity3D. The main scene is the board, and has the data about each player and the current (randomly-generated) board stored within it.
I intend to add minigames into the game, for example when landing on a particular space on the board. Naturally, I would like to code the minigame in a separate scene. Is there a way I can do this without losing the instance of the current scene, so that the current scene's state is maintained?
Thanks in advance :)
Short answer: no, but there may be another way to do what you want.
A basic call to Application.LoadLevel call will destroy the current scene before loading the next one. This isn't what you want.
If your minigame is relatively simple, you could use Instantiate to bring in a prefab and spawn it far away from the rest of your scene. You can even use scripts to switch to another camera, toggle player controls and other interactions in the scene, and so on. Once the minigame is done, you can destroy or disable whatever you brought in, and re-enable whatever needs to be turned on in the main scene.
You could create a separate scene and call Application.LoadLevelAdditive to load that scene without destroying the current one. As above, you can then use scripts to manage which cameras and scene behaviors are active.
If you're careful, you don't really need two separate scenes. It may be enough to "fake" a scene switch.
Hard to give a complete answer without code, but you should look into the following things either with the unity documentation or youtube:
PlayerPrefs, this is one way of saving data, although i believe it isn't entirely secure i.e. being able to edit from a text file.
Serializable, this is apparently better than playerprefs.
DonDestroyOnLoad, can carry over information to multiple scenes.
Static variables, again not sure if this will help your particular problem.