Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

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:
    5
    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. LurkingNinjaDev

    LurkingNinjaDev

    Joined:
    Jan 20, 2015
    Posts:
    1,906
    What is the type of this variable in C#?
     
  3. misnina

    misnina

    Joined:
    Jul 23, 2017
    Posts:
    5
    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