Search Unity

Instantiating a button and modifying its script?

Discussion in 'Scripting' started by Rotavele, May 30, 2016.

  1. Rotavele

    Rotavele

    Joined:
    Jan 28, 2016
    Posts:
    29
    Hello all,

    Basically what I want to do is instantiate around 100 buttons, but each have a different option pulled from a WWW.

    What I was going to do is make a button prefab and set a script with it that holds the buttons ID and text. How would I edit the ID via script for each one? I'm familiar with the WWW part and instantiating it.
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,521
    Put the button text and WWW target in string variables, then set the strings when you construct the buttons at runtime.
     
  3. Rotavele

    Rotavele

    Joined:
    Jan 28, 2016
    Posts:
    29
    I'm not sure what you mean. I can construct the buttons with their data just fine, but when someone clicks the button I want it to do different things.

    For example it will load 100 different images, and when a user clicks one it will respond by displaying the appropriate data for that image.
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,521
    Erm, I'm not sure what you are trying to do here, then.
     
  5. Rotavele

    Rotavele

    Joined:
    Jan 28, 2016
    Posts:
    29

    A good example would be a mail box. For each message for the player found in the database I want to instantiate a subject line and also a view button for each message. When the player clicks the view button beside the subject, how do I tell a new WWW to get the message text to display?
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This is pretty much it.

    Have a variable on each button. Set the variable to whatever is appropriate when you instantiate it. Then take the appropriate action when you click.

    This video is somewhat tangential to your question, but it may help.

     
    Rotavele likes this.
  7. Rotavele

    Rotavele

    Joined:
    Jan 28, 2016
    Posts:
    29
    That works great! Thank you so much!!