Search Unity

How can I create a new class named after a string?

Discussion in 'Scripting' started by DocOverbuild3, May 10, 2021.

  1. DocOverbuild3

    DocOverbuild3

    Joined:
    Aug 10, 2020
    Posts:
    6
    Hey everyone, sorry if this is a stupid question in advance, just getting back into C# coding.

    I'm currently dealing with the following script:

    Code (CSharp):
    1.   public void Awake()
    2.     {
    3.       TextAsset productList = Resources.Load<TextAsset>("ProductList/productList");
    4.  
    5.       string [] productData = productList.text.Split(new char[] { '\n' }); /// Takes the data and removes any line breaks before putting it into a string array. First letter is 0.
    6.  
    7.       for(int i = 1; i < productData.Length - 1; i++) /// Goes through each line
    8.         {
    9.           string[] productRows = productData[i].Split(new char[] { ','}); /// Take out the commas
    10.  
    11.           string productName = productRows[1]; /// Gets the name of the product
    12.  
    13.           RestaurantProduct productName = new RestaurantProduct();
    14.         }
    15.     }

    Everything works all find and dandy until I get to the last part, where I create a new version of my RestaurantProduct class named whatever the productName string is. However, I'm getting an error that says I can't use it since productName is already the name of another scope, which I'm assuming means that instead of the class taking the name of the string, it's taking the name of the variable. So is there any way to name a new version of a class after a string? From what I can tell from the internet, I might be able to use something like CreateInstance, but I can't understand the documentation for it so I was wondering if that's even the right direction to go in. Would really appreciate any advice on how to fix this, or even if what I'm trying to do is possible
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Line 11 and Line 13 just both create variables that use the identifier productName, that's all your problem is. Fix that.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    Your question is worded confusingly, but your problem is simply that you are trying to create two variables with the same name:
    upload_2021-5-9_20-37-41.png

    I'm not really sure what you're trying to do but I would just expect something like this:
    Code (CSharp):
    1. string productName = productRows[1]; /// Gets the name of the product
    2. RestaurantProduct product = new RestaurantProduct(productName); // assumes you have a constructor that accepts a name as string.
    3. // OR something like this:
    4. RestaurantProduct product = new RestaurantProduct();
    5. product.name = productName;
     
    Last edited: May 10, 2021
  4. DocOverbuild3

    DocOverbuild3

    Joined:
    Aug 10, 2020
    Posts:
    6
    Well that's the issue. I want the new instiation of the class to be called whatever the string productName is. So, if productName is say Hamburger, a new RestaurantProduct class named Hamburger is created.
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    This doesn't make any sense. What do you mean by a new class called Hamburger? You seem to be confused about the relationship between classes (which are essentially blueprints for an object) and instances of a class which is what you get when you call
    new RestaurantProduct()
    .
     
  6. DocOverbuild3

    DocOverbuild3

    Joined:
    Aug 10, 2020
    Posts:
    6
    Yeah, sorry about that, that's what I meant. I'd like to create a new instance of a class named whatever the productName string is. So, to go with my confusing example above, if productName is Hamburger, then the script creates a new instance of the RestaurantProduct class named Hamburger.

    I appreciate your help btw, I know this is some rudimentary stuff but it's been a hot minute since I last booted up Unity lol.
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    Then your RestaurantProduct class needs to have a field or a property called "name" or "Name" or whatever you want to call it, and you do something like in my example here: https://forum.unity.com/threads/how...ss-named-after-a-string.1107239/#post-7124492

    For the first example you would need to have a constructor that accepts the name as a parameter. For more info on constructors see here: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constructors
     
  8. DocOverbuild3

    DocOverbuild3

    Joined:
    Aug 10, 2020
    Posts:
    6
    Ah alright, I'll go read that documentation.

    Appreciate you're help, thank you!