Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Assigning skills to UI buttons

Discussion in 'Scripting' started by MrZeker, Jun 8, 2019.

  1. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    Hello
    im searching for the best/optimal way to assign spells to UI buttons:
    i have an array/list that contains the INT identifier of the spells each character has. (1,3,12,7,etc)
    Each characeter has 10 spells. and the UI spellbook has 10 buttons which previously were hardcoded to each spell, but now that i have more characeters i want different characters to have different spells.
    My question here is. what would be the optimal way to change the buttons in the UI to the character skills (which are located in Scriptable objects). This is the kind of idea that i have come across, but im sure there might be better ones:

    On start function it will load the Scriptable object spell list, and each spell slot (1 to 10) will have a variable:
    Spell1ID = X; that will get the number on the list of the ID of the spell, and use a Switch for changing the Button UI text to the name of the spell. When the button is pressed it will use a Switch that call another function (each spell is a function) based on the SpellID.

    the problem i see is that i will have to put the same switch on every button (10 buttons), and it will make the code unnecesary long. Any better ideas?
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Why not just hold a reference to the assigned spell SO in the button and reference the data directly from that / fire the ability execute function when clicked?
     
  3. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    That's a great idea.

    So i tried it, and im having a problem right now, that i cant really figure out the solution:
    Code (csharp):
    1.  
    2.     void CharacterSpellSetting()
    3.     {
    4.  
    5.         PSpell1 = Wardrobe.PlayerSpell1;
    6.        Spell1Text.text = PSpell1.Spellname;
    7.    //     Spell1Text.text = "" + PSpell1.SpellID;
    8.  
    9.     }
    10.  
    This doesnt work and im not sure why. The Spellname is an String. and Spell1text is a UItext. i tried using ToString(). but nothing happens, however, the SpellID displays correctly, so the game is reading the Scriptable object for the spell, but it isnt reading the string? im sure this is a formatting mistake, but cant find it yet.
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Check make sure you didn't accidentally make Spellname an auto-property, instead of get/setting the serialized field. I do that sometimes on accident and it's sort of annoying. Beyond that, you'll have to show where Spellname is being defined and the contents of whatever class PlayerSpell1 is- either the getter is not set up right (in the case of it being a property) or it's being overwritten before you access it for some reason.
     
  5. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    im using a static variable for both the spellID and Spellname (because i dont really know other way to do it right now)
    the weird thing is one works and the other doesnt.
     
  6. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    help?