How do I convert a char array to string (C#)?
I have a problem. I am trying to create an on-screen keyboard, and right now I am just testing how to create a string from the characters the player inputs. Here is the code, using UnityEngine; using System.Collections; public class OnScreenKeyboard : MonoBehaviour { %|-2027507329_1|% %|134211281_2|% %|703842105_3|% %|1650434119_4|% %|-2095084726_5|% %|1016882716_6|% %|-415537406_7|% %|102902370_8|% %|2047609155_9|% Input[6] = 'W'; %|-270864002_11|% %|-1309702685_12|% %|-963489900_13|% %|-641054115_14|% %|-1870234116_15|% %|-1626960618_16|% } }
It should log the string HELLO WORLD, but it returns a nullreferenceexception.
NullReferenceException: Object reference not set to an instance of an object OnScreenKeyboard.Start () (at Assets/Resources/Script/OnScreenKeyboard.cs:7)
If anyone one can help, I would appreciate it.
It seems that the code is not posting correctly, here it is
using UnityEngine;
using System.Collections;
public class OnScreen$$anonymous$$eyboard : $$anonymous$$onoBehaviour
{
static char[] Input;
void Start()
{
Input[0] = 'H';
Input[1] = 'E';
Input[2] = 'L';
Input[3] = 'L';
Input[4] = 'O';
Input[5] = ' ';
Input[6] = 'W';
Input[7] = 'O';
Input[8] = 'R';
Input[10] = 'L';
Input[11] = 'D';
string Output = new string(Input);
Debug.Log(Output);
}
}
You didn't initialize the size of the array.
Like Sergio7888 said, you missed one index.
And the array must be ter$$anonymous$$ated with a 0-byte.
Answer by Sergio7888 · Sep 10, 2016 at 06:52 AM
You skipped Input[9], so index 9 is null.
Try use System.Text.StringBuilder