Search Unity

Mapping enum to a text object?

Discussion in 'Scripting' started by I-Like-Dogs, Aug 11, 2019.

  1. I-Like-Dogs

    I-Like-Dogs

    Joined:
    Jul 23, 2019
    Posts:
    9
    Hi Everyone,

    I hope you all had a nice weekend, unfortunately my Bearded Dragon died. I found him this afternoon bless him.
    I'm working on my project to keep my mind busy and wondered if anyone can help please as I am a novice and quite stuck.

    I have an RPG type game, I have scripts for equipping items, unequpping them and deleting them from the character bag, thanks in large to online tutorials and a lot of tinkering. what i have tried to create this afternoon is a character GUI where if i equip an item i want the name of that item to show in the correct slot on the character GUI.

    the items themselves have an enum ( you will see in the scripts below). what i want is when the equip script is run, i wan that item to look at its own enum and map to the correct text object in the character GUI i.e. if i equip a sword I want that swords name (for example BasicSword) to go in the sword slot, not the shield, leg, chest etc slot.

    I have no idea how to do this, I have no idea how to map the text objects to the enum, and i have no idea how to get the equip function to do what I have said above, please could people look at my scripts below and help suggest a possible answer? I don't know if i can post images on here, but if needed i can post pictures of the GUI.

    thank you in advance.

    code removed
     
    Last edited: Aug 16, 2019
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    Sorry to hear of your loss, my condolences.

    In general when an item is equipped you would set the text object in the equip method. Access to the text object and the info from the item is all you need. Then you do something like:
    Code (CSharp):
    1. chestText.text = item.name;
    I see that you are firing an event when equipment is changed, so if the GUI is registered for that event you should have everything you need to accomplish this.
     
  3. I-Like-Dogs

    I-Like-Dogs

    Joined:
    Jul 23, 2019
    Posts:
    9
    something like this?:

    Code (CSharp):
    1. equip(this);
    2.  
    3. if item.name == enum sword
    4. {
    5.    textObject.SwordGUI = item.name
    6. }
    7.  
    8. if item.name == enum shield
    9. {
    10.    textObject.ShieldGUI = item.name
    11. }
    12. etc
    13. etc
    14.  
    i know the above is jibberish code but I'm hoping it makes enough sense to see what I'm driving at? how would i pull the enum from the other script to that one?

    thank you for your help :)
     
  4. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    I'm not sure how your code structure works so its hard for me to really say how to get it to work.

    Say the player equips an item from the inventory. How ever this works, right click/drag and drop.
    It will call the EquipmentManager script to equip the item. This is were we get the item from. At this point it depends on how you want to code it but the above pseudo code will work, but dealing with enums you can use a switch statement instead.

    Code (CSharp):
    1. switch (item.equipSlot)
    2. {
    3.      case EquipmentSlot.Head:
    4.           //Do head slot here
    5.           textObject.HeadGUI = item.name;
    6.           break;
    7.      case EquipmentSlot.Chest:
    8.           //Do chest slot here
    9.           textObject.ChestGUI = item.name;
    10.           break;
    11.       ect...
    12. }
     
  5. I-Like-Dogs

    I-Like-Dogs

    Joined:
    Jul 23, 2019
    Posts:
    9

    Just wanted to say thank you for your help. I've gotten it work and I'm very happy with it. as an added extra I learned you can't add a script to an object unless it derives from monobehaviour !

    thank you :)
     
    Chris-Trueman likes this.