Search Unity

  1. Unity 2019.1 beta is now available.
    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. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    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:
    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. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    2,869
    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