In the game scene I have this script :
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.Experimental.GlobalIllumination;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class BackToMainMenu : MonoBehaviour
{
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (Time.timeScale == 0)
{
MoveScrollView.goBack = true;
MoveScrollView.go = false;
SceneManager.LoadSceneAsync(1);
Cursor.visible = false;
Time.timeScale = 1;
}
else
{
Time.timeScale = 0;
SceneManager.LoadSceneAsync(0);
SceneManager.sceneLoaded += SceneManager_sceneLoaded;
Cursor.visible = true;
}
}
}
private void SceneManager_sceneLoaded(Scene arg0, LoadSceneMode arg1)
{
MoveScrollView.go = true;
MoveScrollView.goBack = false;
}
}
Pressing once the escape key it’s loading the main menu scene and pressing the escape key again removing the main menu and loading the game scene.
In the Main Menu scene I have this script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveScrollView : MonoBehaviour
{
float seconds;
Vector3 begin;
Vector3 end;
Vector3 difference;
public static bool go = false;
public static bool goBack = false;
void Start()
{
seconds = 0.5f;
begin = transform.localPosition;
end = new Vector3(0, -1, 0);
difference = end - begin;
}
void Update()
{
if (go)
{
if (transform.localPosition.x < end.x)
{
var mx = transform.localPosition.x;
transform.localPosition = new Vector3(mx += seconds * Time.unscaledTime, -1, 0);
difference = end + transform.localPosition;
}
}
if(goBack)
{
if (transform.localPosition.x > end.x)
{
var mx = transform.localPosition.x;
transform.localPosition = new Vector3(mx += seconds * Time.unscaledTime, -1, 0);
difference = end - transform.localPosition;
}
}
}
}
It’s moving it fine when the main menu scene is loaded when the flag bool variable go is true.
but then when making escape again and loading back the game scene I want before it’s loading the game scene to move it back to it’s original position so I added the part of the code :
if(goBack)
{
if (transform.localPosition.x > end.x)
{
var mx = transform.localPosition.x;
transform.localPosition = new Vector3(mx += seconds * Time.unscaledTime, -1, 0);
difference = end - transform.localPosition;
}
}
Using another flag name goBack.
-
How to make that it will move first to it’s original position then it will load the game scene back ?
The game scene is at index 1
-
Not sure if the code when goBack is true if it’s the right code. Can’t test it because the game scene is loading too fast.