how to get a Int with the same name as a string

i want to get the int value from the int with the same name as my ProductName how do i do it?

It’s possible, but it’ll use reflection and it’s a really terrible way to do things. If you have specific product names that correspond to int values, just use an enum instead, or if the values are variable then use a struct or class to hold a string and an int field, or a dict<string, int>, etc… Hundreds of ways to do it, really.

If you want to make a generic system it sometimes can come in handy to use reflection to retrieve the value from a variable name (this also works with AOT platforms). However: It is not as fast as normal referencing. And in most cases there are better ways to do it (as Lysander mentioned).

Please describe a little bit more what you are trying to do and give us more code (please post the code as code-blocks instead of screenshots). As soon as we gathered enough information we can give you better advise how to solve your problem in an elegant way.

Seems like a use case for a dictionary. Use the product name as a key. Wrap everything else up in some sort of data structure.

And a rubber duck. Just discovered that term and now I am going to use it everywhere.

i want to be able to use a string from Code A to be able to get a int number with the same name as the string from Code B

Code A:

Code B:

so i can use it in a Text. shown in Code A

Do what @BoredMormo said. It’ll be much better than using reflection.

yeah… do something like this:

public enum ResourceType
{
    Iron, Gold, Platinum, Titanium, Wood, Wheat, Fish,
}

and in code B delete the int variables and put there instead the following code:

public Dictionary<ResourceType, int> Resources = new
Dictionary<ResourceType, int>;

void Awake()
{
    foreach(var resource in Enum.GetValues(typeof(ResourceType)))
    {
        Resources.Add((ResourceType)resource, 0);
    }
}

(please note: dictionaries are not supported by the unity editor per default. You can use them, as long as you only access it from code, not the inspector)

in code A replace the following:

// instead of ProductName:
public ResourceType ProductType;

//replace old TradeBought:
public void TradeBought()
{
    if(click.gold >= Cost)
    {
        click.gold -= Cost;
        TCD.Resources[ProductType] += 1;
    }
}

// Edit: almost forgot your actual question ^^
void Start()
{
    ButtonDisplay.text = "Product: " + ProductType.ToString() + "\nCost: " + Cost + "\nOwned: " + TCD.Resources[ProductType];
}

PS: code not tested
PPS: probably “Resources” is a bad variable name, cause it is a reserved Type of Unity…
PPPS: next time no screenshots of your code please… Or my answer will be much shorter (very annoying to type everything instead of copying)

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TradingCenterData : MonoBehaviour {

    public enum ResourceTypes
    {
        Iron, Gold, Platinum, Titanium, Wood, Wheat, Fish,
    }
    void Awake(){
        foreach(var resource in Enum.GetValues(typeof(ResourceTypes)))
        {
            Products.Add((ResourceTypes)resource, 0);
        }
    }


    public Dictionary<ResourceTypes, int> Products = new
        Dictionary <ResourceType, int>
}

it gives a error “(21,1)error cs1519: Unexpected symbol ‘}’ in class, struct, or interface member declaration”

Comma after fish

i think its something with the dictionary

“();” at the end of the dictionary declaration, because the constructor is a function.

public Dictionary<ResourceTypes, int> Products = new Dictionary<ResourceTypes, int>();
1 Like
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TradingCenter : MonoBehaviour {
  
    public int Cost;
    public Click click;
    public string ProductName;
    public TradingCenterData TCD;
    public UnityEngine.UI.Text ButtonDisplay;





    // instead of ProductName:
    public TradingCenterData.ResourceType ProductType;
  
    //replace old TradeBought:
    public void TradeBought()
    {
        if(click.gold >= Cost)
        {
            click.gold -= Cost;
            TCD.Products[ProductType] += 1;
        }
    }
  

    // Edit: almost forgot your actual question ^^
    void Start()
    {
        ButtonDisplay.text = "Product: " + ProductType.ToString() + "\nCost: " + Cost + "\nOwned: " + TradingCenterData.ResourceType[ProductType];
    }










  

//    void Start(){
//        ButtonDisplay.text = "Product: " + ProductName + "\nCost: " + Cost + "\nOwned: " + Input.GetKey;
//    }
//
//  
//    public void TradeBought(){
//            if (ProductName == "Iron"){
//            if (click.gold >= Cost) {
//                click.gold -= Cost;
//                TCD.Iron += 1;
//            }
//            }
//            if (ProductName == "Gold"){
//                if (click.gold >= Cost) {
//                    click.gold -= Cost;
//                TCD.Gold += 1;
//                }
//            }
//            if (ProductName == "Platinum"){
//                if (click.gold >= Cost) {
//                    click.gold -= Cost;
//                TCD.Platinum += 1;
//                    }
//            }
//            if (ProductName == "Titanium"){
//                if (click.gold >= Cost) {
//                    click.gold -= Cost;
//                TCD.Titanium += 1;
//                        }
//            }
//            if (ProductName == "Wood"){
//                if (click.gold >= Cost) {
//                    click.gold -= Cost;
//                TCD.Wood += 1;
//                            }
//            }
//            if (ProductName == "Wheat"){
//                if (click.gold >= Cost) {
//                    click.gold -= Cost;
//                TCD.Wheat += 1;
//                                }
//            }
//            if (ProductName == "Fish"){
//                if (click.gold >= Cost) {
//                    click.gold -= Cost;
//                TCD.Fish += 1;
//            }
//        }
//    }
}

(34,129): error CS0119: expression denotes a ‘type’, where a ‘variable’, ‘value’ or ‘method group’ was expected

now i get that and if you need my other script just incase

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TradingCenterData : MonoBehaviour {

    public int Iron;
    public int Gold;
    public int Platinum;
    public int Titanium;
    public int Wood;
    public int Wheat;
    public int Fish;

    public enum ResourceType
    {
        Iron, Gold, Platinum, Titanium, Wood, Wheat, Fish,
    }
    void Awake(){
        foreach(var resource in Enum.GetValues(typeof(ResourceType)))
        {
            Products.Add((ResourceType)resource, 0);
        }
    }


    public Dictionary<ResourceType, int> Products = new
        Dictionary<ResourceType, int>();
}

enums are used like this
TradingCenterData.ResourceType.Iron;
But ProductType is already a TradingCenterData.ResourceType
you shouldnt need any more than just “ProductType” (on line 34)

ButtonDisplay.text = "Product: " + ProductType.ToString() + "\nCost: " + Cost + "\nOwned: " + TCD.Products[ProductType];

BTW: “instead of ProductName” means that you delete the ProductName Variable :wink:

Edit: When eventually everything is working I have a task for you:
Go through the code I wrote for you and stop everywhere where you don’t understand what’s going on. search the internet (and the MSDN dokumentation page) to figure out what is happening.
If you don’t understand something or didn’t find an answer after researching: Ask here for clarification.
Write a comment above everything which you researched and describe with your own words, what is going on (this helps to don’t forget it).
This way you can improve your programming skills as well as the ability to solve problems yourself by searching the internet.