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. Dismiss Notice

Question Button.clicked can not be Invoked

Discussion in 'Editor & General Support' started by rekatha, Mar 3, 2023.

  1. rekatha

    rekatha

    Joined:
    Dec 18, 2017
    Posts:
    22
    Hello, I have script that call button click programmatically.
    Something like :
    Code (CSharp):
    1. m_loginButton.onClick.Invoke();
    But then there is this documentation said that onClick is Obsolete and use Button.clicked instead : https://docs.unity3d.com/ScriptReference/UIElements.Button.html

    I try change to Button.clicked but the Button.clicked can not be Invoked
    Code (CSharp):
    1. m_loginButton.clicked.Invoke();
    2. m_loginButton.clickable.clicked.Invoke();
    That each line of code is error "CS0079: The event 'clicked' can only appear on the left hand side of += or -="

    The other strange behavior is, this clicked is part of UnityEngine.UIElements but onClick is part of UnityEngine.UI - So I have to change to using UnityEngine.UIElements. But if I using UnityEngine.UIElements I cannot SerializeField my variable m_loginButton. It won't show in editor so that I can't pass the button in editor.

    So I have 2 question.
    1. How to pass button to SerializeField variable if using UnityEngine.UIElements ?
    2. How to click button programmaticly if use Button.clicked ?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,862
    UnityEngine.UIElements.Button.clicked
    is an event. Events can only be called by their containing class.

    If you want a method to happen both via clicking the button, or via code, just code it into a visual element deriving from
    UnityEngine.UIElements.Button
    .

    It's not strange at all. Visual Elements are not
    UnityEngine.Object
    s. You can't serialise references to them.

    UI Toolkit is a completely different work flow to to the older Unity UI system, fyi. Prepare to completely throw out your usual paradigms if you intend to pursue UI Toolkit.

    I feel like you're getting your UI systems mixed up. It sounds like you should only be using
    UnityEngine.UI
    , and are mistakenly using
    UnityEngine.UIElements
    .
     
    rekatha likes this.
  3. rekatha

    rekatha

    Joined:
    Dec 18, 2017
    Posts:
    22
    Thank you for your explanation.

    yes, seems like I get confuse with this UI systems. So this Button from UnityEngine.UI is different with Button from UnityEngine.UIElements ? And this UI Toolkit is will replace current Unity UI system right ? Do you think that UI Toolkit ready for production ?
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,862
    Both UI systems are completely different and work in vastly different ways.

    Though the old Unity UI isn't going anywhere, same as the built in render pipeline isn't going anywhere for the new render pipeline, and same as how Monobehaviours aren't going anywhere for ECS/DOTS.

    Whether UI elements is suitable for you depends on your project. If you're unsure, stick to Unity UI.
     
    rekatha likes this.