Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Button.onClick Obsolete

Discussion in 'UGUI & TextMesh Pro' started by D3tona, Jul 11, 2023.

  1. D3tona

    D3tona

    Joined:
    Dec 4, 2014
    Posts:
    4
    Hi, today I updated my project from 2021.3 to 2022.3 and get a question.

    Here Unity recommend to use Unity UI for runtime:
    https://docs.unity3d.com/2022.3/Documentation/Manual/UI-system-compare.html

    but here at button say onClick is Obsolete and I should use Ui Toolkit:
    https://docs.unity3d.com/2022.3/Documentation/ScriptReference/UIElements.Button-onClick.html

    So, how do I make a click button at runtime without use Obsolete code?
    Should I use Ui Toolkit on every runtime button to avoid get obsolete?
     
    Snake-M3 likes this.
  2. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    606
    You are mixing up documentation for two different buttons from two different UI libraries.

    You linked to UnityEngine.UiTools.Button.onClick that is the docs for the version of button from Ui Toolkit. That's the event which is deprecated. It doesn't say anything about deprecation of onClick in Unity UI button.

    So if you are using Ui Toolkit you have to use clicked instead of onClick.

    If you are using Unity UI it doesn't apply to you. I am guessing that in your code you probably imported the wrong thing and thus got warning about wrong kind of Button and got very confused.
     
  3. D3tona

    D3tona

    Joined:
    Dec 4, 2014
    Posts:
    4
    You are right, I mixed the documentation... no i'm not getting any error, i'm just reading.

    Still, I was unable to find UnityEngine.UI on documentation 2022

    Found this one:
    https://docs.unity3d.com/Packages/com.unity.ugui@2.0/api/UnityEngine.UI.Button.html
     
    Last edited: Jul 12, 2023
  4. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    606
    https://docs.unity3d.com/2022.3/Documentation/ScriptReference/ Contains documentation only for core unity functionality that's only small fraction of Unity. A lot of Unity functionality is in separate optional packages that can be installed using Unity package manager. Each of those packages often have their own documentation using similar format as one you linked above. You can get the documentation link of most Unity non core packages in the package manager.

    upload_2023-7-12_16-29-59.png
     
    Snake-M3 likes this.