Search Unity

UI: One Button to Open Variable URLS?

Discussion in 'Scripting' started by JefferyWright, Jan 22, 2020.

  1. JefferyWright

    JefferyWright

    Joined:
    Jan 8, 2016
    Posts:
    10
    I have a panel with copy I can populate from a function, the panel also has a button, which depending on the active function, I want to open a specific URL.

    The panel button is specified via:
    Code (csharp):
    1. public Button getUrl;
    An example of a typical function is:

    Code (csharp):
    1.  public void f_B01()
    2.     {
    3.         DisplayPanel.SetActive(true);
    4.         ProductName.text = "Cylinders and Actuators";
    5.         ProductDesc.text = "The leading global manufacturer of hydraulic cylinders.";
    6.        // This doesn't work:
    7.         getUrl.OpenURL("http://website01.com/");
    8.      
    9.     }
    Each function has a unique URL, how do I get the panel button to get the url in each function and open it?

    This is the current error I'm getting:

    error CS1061: 'Button' does not contain a definition for 'OpenURL' and no accessible extension method 'OpenURL' accepting a first argument of type 'Button' could be found (are you missing a using directive or an assembly reference?)

    Thanks.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You're trying to get the button component to open the URL, and the button component doesn't have that ability. Try Application.OpenURL instead.
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    I assume you mean that you want to open that URL when the button is clicked, in which case you'll need to write a function for the button to call when it's clicked, assign that function to the buttons OnClick event, and write the function such that it accesses the currently-appropriate URL (presumably stored in a variable somewhere) and calls Application.OpenURL() or something similar.