Instantialising some mesh over an already instantialised mesh based on a string.

I might find a solution soon, but for now it seems like I’m hitting a wall which doesn’t seems to normal.
I’m currently working on the creation process for a action RPG project of mine and the process of creating a profile (character) is a bit more complex than just filling a form as the background of the menu gets stuff added into it as the player choose in displayed choices (or enter text in text fields).
Right now, the thing that is blocking me is related to the text field for the name of the profile.
I’m trying to instantiate real 3D meshes representing each letters as they are typed into the text field.
The position of each letters varies based on a previous choice made by the player as the asset on which the 3D text meshes appear change.

This is the part of the code which I am using related to the 3D text generator.
Whenever (and once in the awake) the text field on which the player type its profile name change, this function is being called. I added a comment on almost every line to allow you to understand how it’s used.

This is not the whole bunch of code, but only represent the part called whenever there’s a change a Textfield form.

void UpdateProfilName()
    {
        Debug.Log("Profil name is being updated");
      
        //This is to make sure that the profil name never return null or empty
        if(CurProfilName == "")
        {
            CurProfilName = " ";
        }
        /* ProfilName is the final public string where CurProfilName is the string that get modified. For example, as CurProfilName is being typed in the Textfloat, it gets transformed .toUpper() while typed.*/
        ProfilName = CurProfilName;
      
        foreach (BG c in BGModels) {
            /*This is so that only the selected BG have its additionnal assets generated. In the previous option, the SelectedBG was switching based on buttons and key input.*/
            if (c.BgID == SelectedBG) {
            //This is just a little safeguard to make sure there is no error message whenever the player erase all the textfield's characters.
            if(ProfilName[0]!=null){
                /*This allow access to the children of the instantiated BG which represent each letter position. Each selectable BG got their own pos_Letters_1 to pos_Letters_10 as childs. This allow me an easy way of customizing the available BG and the position of everything.*/
                foreach(Transform child in c.Instance.transform)
                    {
                    //For the sake of not putting 10 times the following lines, I just put pos_Letters_1 This goes from 1 to 10 in the actual long script
                    if(child.gameObject.name == "pos_Letters_1")
                    {
                        //This gives me the transform values for the letter to be instantiated. Debugged and works fine
                        c.Letter01 = child;       
                    }
                }
                        //This is so that I can serialize each possible letters and their related 3D mesh.
                        foreach(BGLetter i in BGLetters)
                        {
                            if(i.Letter.Equals(ProfilName[0]))
                            {
                                /*
                                This is where it doesn't seems to work. I have tried to debug if i.Letter return the the available characters (A-Z) from the serialized class and it does. I have tried to debug if ProfilName[0] return the right characters. It does and I get all the first character that is entered via the text field. But Unity never seems to return .Equals as true even of both have the same type of strings.
                                */
                            }
                        }
            }
            }
        }
    }

Since it can get hard to read with all the comment I placed (to make you understand how it’s used), this is the code without comments :

void UpdateProfilName()
    {
        Debug.Log("Profil name is being updated");
     
        if(CurProfilName == "")
        {
            CurProfilName = " ";
        }

        ProfilName = CurProfilName;
      
        foreach (BG c in BGModels) {
            if (c.BgID == SelectedBG) {
            if(ProfilName[0]!=null){
                foreach(Transform child in c.Instance.transform)
                    {
                    if(child.gameObject.name == "pos_Letters_1")
                    {
                        c.Letter01 = child;
                    }
                }
                foreach(BGLetter i in BGLetters)
                        {
                            if(i.Letter.Equals(ProfilName[0]))
                            {
                                /*Part that doesn't work as it never return true*/
                            }
                 }
            }
            }
        }
    }

This might be just a simple thing like if I had to use something else then .Equals() but everywhere I check, this is what is said to be used in an if statement for a Boolean comparison between 2 strings so that whenever both are equal/same, it returns as true.

For some reason, in this case, even of both strings are identical (verified the values with debug), it doesn’t return as true.

This is how it was supposed to work :

  1. First, the system gets which BG is being visualized via the SelectedBG int. (This works)
  2. For the single BG which it returns true, the system looks into the instance’s children and look for the game object with the name “pos_Letters_1”. and get the transform values for the letters position (This works)
  3. As each BGLetter in the of BGLetters is being read by the system with the foreach, whenever a letter ID (string called “Letter” available for each BGLetter in the List) is the same to a specific letter in the string from the text field (ProfilName[0]), I could then call whatever function I would wish, like an instantiation of a mesh from the BGLetter’s serialized class.

Again, just in case some didn’t read everything and jump to conclusion that I can’t generate multiple letters as I just show, here, only one position/transform and only take into consideration the first letter of the text field (ProfilName), it’s because adding the whole bunch of lines would be of little importance since it would be “almost” the same lines for all the available space in the Text field (which is limited to 10 characters).
Only the following bits would change.
pos_Letters_1 => pos_Letters_2
Letter01 => Letter02
ProfilName[0] => ProfilName[1]

Thanks in advance!

The issue is that you are not comparing 2 strings with your .Equals
You are comparing a string and a char.
If you had used the == operator it wouldn’t even compile (hinting at the problem).
(this is assuming that i.Letter is a string)

options:

  • i.Letter[0] == ProfilName[0]

  • i.Letter[0].Equals(ProfilName[0])

  • i.Letter.Equals(ProfilName[0] + “”) (make char into string by appending an empty string

  • change i.Letter to be type char

1 Like

Thanks! This seems to be what was missing.
Didn’t knew that there was a difference between a string[×] and a string, with the exception of one being partial. (Thought both of them would be seen as strings)

Out of curiosity, if I was to compare more than one letter in a string, would the following method be correct?
For example the 3 first letters of a string…
string[0]+string[1]+string[2]
or would it something like…
string[0-2]
or…
string[0,1,2]

I was wondering since it’s not covered in the Unity’s community wiki and since, to be honest, the best I could find related to the subject is the string(char[×]) which is when the string is a class by itself. Google really doesn’t like the and tend to ignore it or change it to ( ), so it makes the search overly difficult.

Thanks for the previous answer! This second one is just for the sake of understanding the string[×] a bit better. :slight_smile:

A string is just an array of chars in the background, which is why you can index them string[0]
You cannot string[0] + string[1] because then you are adding chars (which behave like ints)
Unless you start with a string, and then try to add chars to it ( “” + string[0] + string[1] ) because then it knows you are working with string
There are probably also methos in System.Convert to help

If there isn’t anything in the Unity help pages, its because this isn’t a unity specific thing, it’s just how strings are