Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Static Bool - Detect if a button was clicked - Please help :)

Discussion in 'Scripting' started by Mehdi85, Aug 4, 2020.

  1. Mehdi85

    Mehdi85

    Joined:
    Apr 23, 2020
    Posts:
    7
    Hello all,

    I have a button in my scene that I will need to check if it was clicked. If the button as clicked and if the videoPlayer frame is at 350 I want to change the scene. I have the videoframe part figured out, I just need to check if the button was clicked.

    Here is my handle button that is attached to a standard On Click UI Button.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class HandleButton : MonoBehaviour
    6. {
    7.     public static bool clicked = false;
    8.  
    9.     void LateUpdate()
    10.     {
    11.         clicked = false;
    12.     }
    13.  
    14.     public void Click()
    15.     {
    16.         clicked = true;
    17.        // Debug.Log("CLICKED");
    18.     }
    19. }

    And here is the script that will check the video player frame and if the button was pressed.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Video;
    5.  
    6. public class operationsTentButtonScript : MonoBehaviour
    7. {
    8.     HandleButton handleButton;
    9.  
    10.     VideoPlayer vp;
    11.  
    12.     void Start()
    13.     {
    14.         vp = GetComponentInParent<VideoPlayer>();
    15.         vp.Play();
    16.  
    17.         handleButton = GetComponent<HandleButton>();      
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.         if (vp.frame == 350 && HandleButton.clicked == true)
    23.         {
    24.             //Debug.Log("TEST");
    25.             UnityEngine.SceneManagement.SceneManager.LoadScene("operationsTentSeq");
    26.         }
    27.     }
    28. }

    The line in question is this " if (vp.frame == 350 && HandleButton.clicked == true) "

    What am I doing wrong please. It is driving me crazy lol

    Thank you all.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Unless you click this PRECISELY on frame 350, it won't work: the click will be set and then cleared in LateUpdate

    Why not check if
    vp.frame >= 350
    ?

    Also, don't clear it in Update, because what happens if this Update has already run when the true gets set?

    Instead, clear the boolean back to false after you test and act on it too.

    Here is some timing diagram help:

    https://docs.unity3d.com/Manual/ExecutionOrder.html
     
  3. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    Try remove lateupdate click = false and put it in the if statement instead
     
  4. Mehdi85

    Mehdi85

    Joined:
    Apr 23, 2020
    Posts:
    7
    Thanks for your reply. I want to change scene precisely at frame 350 which first part of the code works just fine.
    If I have just the following it automatically triggers the SceneManager.LoadScene at frame 350. I just need to add the second condition (was the button pressed) in the if statement. Basically eventually this should do the following; if they have pressed it it will load a difference then if they have not at frame 350.





    Code (CSharp):
    1.         if (vp.frame == 350)
    2.         {
    3.             //Debug.Log("TEST");
    4.             UnityEngine.SceneManagement.SceneManager.LoadScene("operationsTentSeq");
    5.         }
     
    Last edited: Aug 5, 2020
  5. Mehdi85

    Mehdi85

    Joined:
    Apr 23, 2020
    Posts:
    7
    Thanks. I will try later today and see what I will get.