Hi,
I’m building a game, where you have to hold a button as long as you can.
But my problem is, you have 3 scenes and 1 button that’s move in all the scene with dontdestroyonload,
But i can’t get that if the player in scene 1 press the button that if it released in scene 2 he stops.
Now you have to push down in scene 2 another time to stop.
So you have to push in scene 1 to start, release in scene 2 to stop. Does somebody know how to do this?
This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Play : MonoBehaviour
{
private static Play instance;
public void Awake()
{
if (instance != null)
{
Destroy(gameObject);
}
DontDestroyOnLoad(transform.gameObject);
}
public void Update()
{
if (Input.GetMouseButtonDown(0))
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
else if (Input.GetMouseButtonUp(0))
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
Thanks Sam
Put the UI canvas stack that controls that button in question into a separate hierarchy under its own Canvas.
Then when the game Start()s up, mark the root GameObject of that canvas hierarchy with DontDestroyOnLoad() so it survives from scene to scene.
Finally when your game is over, explicitly Destroy() the root GameObject so it goes away until you need it for the next game.
Hi @Kurt-Dekker ,
thanks for your reply!
I have another idea, I made a scriptmanager with the scripts Play and Stop.
This is the ScriptManager script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScriptManager : MonoBehaviour
{
public Play ascript;
public Stop bscript;
private static Play instance;
public void Awake()
{
if (instance != null)
{
Destroy(gameObject);
}
DontDestroyOnLoad(transform.gameObject);
}
void Start()
{
ascript = gameObject.GetComponent<Play>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
ascript.enabled = !ascript.enabled;
}
if (Input.GetMouseButtonUp(0))
bscript.enabled = !bscript.enabled;
}
}
If the player clicks it works but if he releases not…
Here the script from play and stop:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Play : MonoBehaviour
{
public void Update()
{
if (Input.GetMouseButtonDown(0))
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Stop : MonoBehaviour
{
public void Update()
{
if (Input.GetMouseButtonUp(0))
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
But it doesn’t work, it works if a ascript is enabled then is bscript disabled and reverted.
Thanks Sam