Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

If mousebuttondown game Start if mousebuttonup games stops

Discussion in 'Scripting' started by SamVorst, Dec 22, 2019.

  1. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6.  
    7. public class Play : MonoBehaviour
    8. {
    9.     private static Play instance;
    10.     public void Awake()
    11.     {
    12.         if (instance != null)
    13.         {
    14.             Destroy(gameObject);
    15.         }
    16.         DontDestroyOnLoad(transform.gameObject);
    17.     }
    18.  
    19.     public void Update()
    20.     {
    21.         if (Input.GetMouseButtonDown(0))
    22.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    23.         else if (Input.GetMouseButtonUp(0))
    24.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    25.     }
    26. }
    27.  
    Thanks Sam
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    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.
     
  3. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ScriptManager : MonoBehaviour
    6. {
    7.     public Play ascript;
    8.     public Stop bscript;
    9.     private static Play instance;
    10.  
    11.     public void Awake()
    12.     {
    13.         if (instance != null)
    14.         {
    15.             Destroy(gameObject);
    16.         }
    17.         DontDestroyOnLoad(transform.gameObject);
    18.     }
    19.  
    20.     void Start()
    21.     {
    22.         ascript = gameObject.GetComponent<Play>();
    23.     }
    24.     void Update()
    25.     {
    26.         if (Input.GetMouseButtonDown(0))
    27.         {
    28.             ascript.enabled = !ascript.enabled;
    29.         }
    30.         if (Input.GetMouseButtonUp(0))
    31.             bscript.enabled = !bscript.enabled;
    32.     }
    33. }
    34.  
    If the player clicks it works but if he releases not..

    Here the script from play and stop:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6.  
    7. public class Play : MonoBehaviour
    8. {
    9.     public void Update()
    10.     {
    11.         if (Input.GetMouseButtonDown(0))
    12.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    13.     }
    14. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class Stop : MonoBehaviour
    7. {
    8.     public void Update()
    9.     {
    10.     if (Input.GetMouseButtonUp(0))
    11.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    12.     }
    13. }
    14.  
    15.  
    But it doesn't work, it works if a ascript is enabled then is bscript disabled and reverted.

    Thanks Sam