Search Unity

[SOLVED]Find variable from string? GetComponent<Script>()."the component in a string";?

Discussion in 'Scripting' started by Infinity_way, Jul 31, 2017.

  1. Infinity_way

    Infinity_way

    Joined:
    Mar 21, 2017
    Posts:
    17
    Hello! i'm having with a very simple problem with this:

    i have a list of slots:
    SlotItem1
    SlotItem2
    SlotItem3
    SlotItem...

    i thought it would be easier to search for a specific slot building the string with it's name:
    Code (CSharp):
    1. string GetSlotItem = "SlotItem" + RegEmptySpot;
    Where the "RegEmptySpot" is the number, so i would ask something like:
    Code (CSharp):
    1. ThisPlayer.GetComponent<Inventoryv2>().GetSlotItem();
    Where the "GetSlotItem" = SlotItemX.

    what is the right way of doing this?

    Thanks in advance.
     
  2. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    Personally I wouldn't usually do it like that.

    I might get a slot by index (in the list).

    Or maybe have each slot know its own number and then iterate through all slots looking for that number.

    Or maybe even have a Dictionary that maps numbers to slots and access in that way.
     
    Kiwasi and TaleOf4Gamers like this.
  3. Infinity_way

    Infinity_way

    Joined:
    Mar 21, 2017
    Posts:
    17
    heard of that solution when i was googling it, i have no idea yet how to use that Dictionary function lol
    but i didn't thought it'd be necessary since i wouldn't uso more than 6 slots.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    It sounds like you're really accessing these by a number. In which case, you definitely should use an array. Unity has a good tutorial for arrays.
     
    Infinity_way and Kiwasi like this.
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    In that case you don't want to be using reflection, which is the other common way to access data via a string.

    But seriously, just go with an array. String based programming is evil.
     
    Infinity_way likes this.
  6. Infinity_way

    Infinity_way

    Joined:
    Mar 21, 2017
    Posts:
    17
    Thanks for the feedback ! i'll look into it! for what i see it is indeed very useful, makes me think about the code i already applied ... like i could have done it smaller and more optimized ... well, it happens, it's not too late for changes XD

    Thanks.
     
  7. Infinity_way

    Infinity_way

    Joined:
    Mar 21, 2017
    Posts:
    17
    Thanks for the replys! it worked! i'm making a multiplayer co-op so its works but ... well :D not perfect yet.
    Just to leave some answer to help someone who might need this.

    Do array. That's it.
    Code (CSharp):
    1. //You can make it as in int, string, bool, whatever.
    2. //The [4] in this case represents the number of elements inside this array.
    3.  
    4. private string[] SlotItem = new string[4];
    5.  
    6. //To check this array, i did this:
    7. int RegSlotHave = -1;
    8.  
    9. if (ObjName == SlotItem [0]) {
    10.                 RegSlotHave = 0;
    11.             }
    12.             if (ObjName == SlotItem [1]) {
    13.                 RegSlotHave = 1;
    14.             }
    15.             if (ObjName == SlotItem [2]) {
    16.                 RegSlotHave = 2;
    17.             }
    18.             if (ObjName == SlotItem [3]) {
    19.                 RegSlotHave = 3;
    20.             }
    21.  
    22. //And then did something like this:
    23.             if (RegSlotHave == -1)
    24. {
    25. //Do stuff
    26. }
    27.  
    28. //To edit or retrieve a value from the array, i do something like:
    29. SlotItem[RegSlotHave] = string x
    30.  
    31. //or
    32.  
    33. string x = SlotItem[RegSlotHave]
    34.  
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    And now you should turn that repetitive code into a loop!
    Not only is it less code, it also makes your code more versatile and much easier to maintain and to read.
    Code (csharp):
    1. for (int s=0; s < SlotItem.Length; s++) {
    2. if (ObjName == SlotItem[s] ) {
    3. RegSlotHave = s;
    4. }
    5. }