How to Send message to another scene - unity3d

I have two scenes in my game(Main Menu and Game).
There are two buttons in Main Menu each sending different values to Game.
So how can i set this value in other scenes script.

If I understand correctly you are trying to pass settings between the main menu scene and the game scene.
What you can do is create a GameSettings script that contains all your settings, and stick it on a game object of your main menu scene.
Store your settings in this script.
Loading the game scene would normally destroys all existing game objects, but Unity provides the Object.DontDestroyOnLoad() function that will prevent an object from being destroyed at loading.
Use this on your GameSettings script and it will exist in both the main menu and game scene and you will be able to retrieve your stored settings from the game scene.
Check the documentation for more info: http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
Here is an example:
public class GameSettings : MonoBehaviour
{
public int NumberOfPlayer;
/* Add other game settings here ... */
void Awake()
{
DontDestroyOnLoad(transform.gameObject)
}
}
Hope it helps :)

Any variable declared static in a script will be preserved across scene changes. If you have a script in both scenes, and set the value of a static variable in one of the scenes, the script instance in the other scene will access the same value.

Related

Loading a scene in Unity twice causes some game objects to lose some components

When I hit play the game scene behaves as normal, even if I come from a different scene.
But when I am on the game scene and go to other scene and back again to the game scene some components from some game objects.
I am using SceneManager.LoadScene("SceneName"); to move around scenes, and every component was added in the editor and saved in the scene.
And as an example here is the Game Manager inspector at first load of game scene:
And second load of game scene:
Windows Controls is a script much like the others missing.
Am I doing something wrong?
Thanks in advance.
Without knowing what's going on inside the GameManager and the FoodManager scripts, I think you use the DontDestroyOnLoad similar what is showed on the bottom of the page, and your code destroys the scripts.
Simply put, I was short-sighted, I used a singleton pattern to access the managers and the way I did, at least, doesn't work in this context.
Any manager that I needed access I created a static instance, but since I checked for its existence elsewhere, the second time the component was destroyed.
public GameManager Instance {get; private set;}
void Awake()
{
if(Instance is not Null && Instance != this)
{
Destroy(Instance);
return;
}
Instance = this;
}
I realize that every time the scene loads the components are new objects, but I am still to understand why there is still copies of the old components.
I fixed it by using the [SerializeField] on any manager reference that I could and and manually adding them on the inspector, for some game objects that were instantiated at run time I pass the manager in a method.
GameManager gameManager;
public void Manager(GameManager _gameManager)
{
gameManager = _gameMananger;
}
GameObject gameObject = Instantiate(prefab);
gameObject.GetComponent<Script>().Manager(this);

Unity Navmesh bake multiple maps in the same scene

I have an scene with multiple maps, and when the game starts I randomly activate one of the maps and keep the others inactive.
My problem is that I can only have one the maps baked, because when I change to bake other map it's overwrited by the previous bake.
I search for other post to try baking at runtime but is seems it's not possible.
Is there a way to have multiple bakes or to bake only the active map at runtime?
To solve the problem, you can call the bake navigation code after the level changes.
Bake Navigation Inside Unity Editor
This code works similar to what is in the Unity Editor. Just make sure your Navigation Static object is enabled before using it.
The NavMeshBuilder class will allow this. In the code bellow.
using UnityEditor.AI;
...
public void Generate()
{
GenerateLevel(); // for e.g
NavMeshBuilder.BuildNavMesh();
}
Bake Navigation in Runtime
To bake in runtime, you need to download the necessary NavMeshComponents.
The component's will give you a NavMeshSurface component. It does not require static navmesh and works locally. Add component to all of your game ground's then put them in a list as the code bellow. After each run of the game, it is enough to BuildNavMesh all or part of them.
public List<NavMeshSurface> surfaces;
public void Start()
{
GenerateLevel(); // for e.g
surfaces.ForEach(s => s.BuildNavMesh());
}
Also this tutorial from Brackeys will help you so much.

Multiple scenes playing at once in Unity

So the problem is i've just added a simple menu system to my game and it seems to load both scenes at the same time. The game is running in the background which is annoying, but somehow after pressing play it resets the game scene and starts playing the game normally. In the build settings the gamescene is named pretty weirdly, but every time i drag my game scene from the hierarchy, it gives me that name in the build settings. Any tips on how to make the scenes work properly?
public class MainMenuN : MonoBehaviour
{
public void PlayGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
public void QuitGame (){
Debug.Log("QUIT");
Application.Quit();
}
}
Well, you have currently both scenes loaded in the Hierarchy!
So when pressing play to enter the playmode both scenes are loaded and running.
Then after pressing the button your do SceneManager.LoadScene without passing in the mode parameter, so it uses the default mode which is Single.
Closes all current loaded Scenes and loads a Scene.
That's why it works after that since this makes all currently loaded scenes be unloaded and the target scene loaded.
This Multi Scene Editing is a pure editor feature and will not behave like that in a build where only the very first scene from the settings will be loaded.
Especially be careful with cross scene references!
For testing correctly in the editor and replicate the same behavior your app will have after building save and then remove the second scene (Ropesgame) from the Hierarchy.

How can I access an object's components from a different 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

Teleport Point: Switch to new scene

I have used the following site to set up the teleportation in my game: https://unity3d.college/2017/05/16/steamvr-locomotion-teleportation-movement/.
As seen from the image, i have inputted 'Location3' as the new scene i would like to teleport to in the build settings. However, when i run the .exe file, i am not able to teleport to a 'Location3' even though I am able to teleport to the other teleport points to a new location in the current scene.
The console logs the "TeleportPoint: Hook up your level loading logic to switch to new scene: Location3".
What you are trying to achieve seems to be a scene change (?)
This can be done using
Application.LoadLevel("Location3");
When you change to this scene you might want to manipulate the position of whatever it is that you want to teleport. This could be achieved by using a static class that does something depending on what scene you are changing too.
I do not recommend to use Application.LoadLevel(), because that function is obsolete and won't be supported in future Unity releases.
You can use:
SceneManager.LoadSceneAsync("NameOfScene");
In order not to keep your Teleportation script from being destroyed you can add the following script to your Teleportation Component.
[Serializable]
public class KeepOnSceneChange : MonoBehaviour {
public void Awake() {
DontDestroyOnLoad(this.gameObject);
}
}
Using this Component your object won't be destroyed when a new scene is loaded and therefor can be used to teleport.