Search Unity

Basic Question: How to get playInventory.itemName should = playerInventory.brightSeeds or else

Discussion in 'Scripting' started by misnina, Dec 8, 2018.

  1. misnina

    misnina

    Joined:
    Jul 23, 2017
    Posts:
    7
    In Javascript, I know how to do this:

    Code (JavaScript):
    1. itemname = brightseeds;
    2. playerInventoryScript[itemname]
    will yeild playerInventory.brightseeds

    I for the life of me, can't figure out how to do with with C# and I don't know the name of it so I can't look for it. I feel silly because I'm sure it's a completely simple thing that I've been overlooking. An example of how I want to use this is

    Code (CSharp):
    1. public void DeterminePlant() {
    2.         if (itemName == "brightSeeds") {
    3.             seeds = playerInventoryScript.brightSeeds;
    4.         }
    5.  
    6.         if (itemName == "darkSeeds") {
    7.             seeds = playerInventoryScript.darkSeeds;
    8.         }
    9.  
    10.         if (itemName == "blueSeeds") {
    11.             seeds = playerInventoryScript.blueSeeds;
    12.         }
    13.     }
    then from then on, I want to be able to call either playerInventory.seeds or go further and just do playerInventory.itemName in the first place to increment or decrement the amount of that particular seed from the inventory. itemName is a public variable that gets assigned to each item.

    Thank you for any help you can provide.
     
    Last edited: Dec 8, 2018
  2. What is the type of this variable in C#?
     
  3. misnina

    misnina

    Joined:
    Jul 23, 2017
    Posts:
    7
    I forgot to write it as playerInventoryScript, it's calling the script and then finding the int variables inside of it.

    Code (CSharp):
    1. private GameObject player;
    2. private PlayerInventoryController playerInventoryScript;
    3. ----
    4. player = GameObject.Find("Player");
    5. playerInventoryScript = player.GetComponent<PlayerInventoryController>();
     
    Last edited: Dec 8, 2018