Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to call a string as a word in quotation marks?

Discussion in 'Scripting' started by JDVDeisgn, May 17, 2018.

  1. JDVDeisgn

    JDVDeisgn

    Joined:
    Oct 17, 2015
    Posts:
    148
    I want to call the text from different strings that all begin with s but have a number on the end, as this would save a lot of coding.

    Code (CSharp):
    1. tt.text = "s"+e;
    Doing it this way ^ just gives me the text s1 or s whatever number e is at that moment in the game.

    Code (CSharp):
    1. tt.text = s+e;
    If I do it this way, I get error CS0019: Operator '+' cannot be applied to operands of type 'method group' and 'float'.

    I've tried to convert e to a string value as well but that didn't work either. I know there must be a way to do this but I haven't been able to find an answer as it seems to be quite a niche question.

    Any help would be much appreciated. Thanks.
     
    Last edited: May 17, 2018
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    What are you asking exactly? "s" + e does exactly what it's suppose to. You don't have to toString e here because when you combine it with a string, it gets converted automatically.

    s+e doesn't work if you don't have a value for s. (since s is a variable)

    But I don't understand what you want from your question.

    If you want to just see what strings start with the letter "s" you can use the startsWith to check if it has s. But this is the value of the string variable, not the variable name.

    https://msdn.microsoft.com/en-us/library/ms131452(v=vs.110).aspx
     
  3. JDVDeisgn

    JDVDeisgn

    Joined:
    Oct 17, 2015
    Posts:
    148
    Basically I have many string called s and then a number, for example "s1", "s2", "s3", "s4" etc and I want the text to change to the different strings when called to. The most efficient way of doing this would be so I could change the value of e (which would just be the number I want to add to the end of s instead of making an if statement for every time I want to change the text).

    I was able to do this when calling the names of GameObjects a while ago but obviously doing that you're calling for the name of an object and not a string which made that a lot easier.

    I just want the
    Code (CSharp):
    1. tt.text =
    to be able to read s(whatever number e is) and call the corresponding string so say e is 5, it would call s5.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    You can't do this

    Code (CSharp):
    1. string s1;
    2. string s2;
    3. string s3;
    4.  
    5. textbox.text = s + e;
    6.  
    If you want to do something similar, you need to use a collection like array/list and then fetch the value by index

    Code (CSharp):
    1.  
    2. public string[] s;
    3. textbox.text = s[e];
    4.  
    5. public List<string> s;
    6. textbox.text = s[e];
    7.  
    Though I would use a better name than "s" for the variable.
     
    angrypenguin likes this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You should just make an array or list of strings, and then address the individual strings by their index number. Try running the following code.

    Code (csharp):
    1. List<string> s = new List<string>();
    2.  
    3. s.Add("I ");
    4. s.Add("Like ");
    5. s.Add("Lists ");
    6. s.Add("Me ");
    7.  
    8. Debug.Log(s[0] + s[1] + s[2]);
    9. Debug.Log(s[2] + s[1] + s[3]);
    You can use public lists and arrays and add their values in the inspector too.
     
    angrypenguin likes this.