Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question The Function Specified in OnClick() Field of a Button Doesn't Work

Discussion in 'Immediate Mode GUI (IMGUI)' started by GerenMeric, Jul 6, 2023.

  1. GerenMeric

    GerenMeric

    Joined:
    May 15, 2023
    Posts:
    47
    Hello,

    I am trying to add a button to start and stop a live feed with a start/stop button. I have written a script to do that. Inside the script i have imported TMPro. Then, i wrote these lines:

    Code (CSharp):
    1. private bool isStopped = false;
    2. public TextMeshProUGUI startstopText;
    Later on, i wrote this function:

    Code (CSharp):
    1. public void OnStartStopClicked()
    2.     {
    3.  
    4.         isStopped = !isStopped;
    5.  
    6.         if (isStopped == false)
    7.         {
    8.  
    9.             startstopText.text = "Stop";
    10.  
    11.         }
    12.  
    13.         else
    14.         {
    15.  
    16.             startstopText.text = "Start";
    17.  
    18.         }
    19.  
    20.     }

    In my code i check if "Stop" is pressed. If it isn't pressed or clicked, i load new texture to a RawImage. But, if the "Stop" button is pressed. Then, it doesn't enter inside the if statement and thus, doesn't run the lines in which i load a new texture to the RawImage.

    After writing my code i assigned the function i have mentioned above to "On Click ()" field of the button from it's inspector.

    But, after playing the scene while i can get the live stream, when i click to the "Stop" button, nothing happens. even, text doesn't change from "Stop" into "Start".

    Could you help me on this issue please?


    Update: I have added these two lines to my code. But, the problem persists:

    Code (CSharp):
    1. Button startStopButton = GetComponent<Button>();
    2. startStopButton.onClick.AddListener(OnStartStopClicked);
     
    Last edited: Jul 6, 2023