Get a Byte from InputField

Hey, I’ve been trying to retrieve the text in an InputField but to no avail. I need a byte (not byte[ ]) because that’s what Photon needs for a maxPlayers function. Any help would be great.

You’re failing to get the text from the input field, or you’re having trouble cutting the retrieved string into bytes?

I can get the text with inputFieldName.text, but I dont know how to cut that string into bytes.

using UnityEngine;
using System.Text;

public class test : MonoBehaviour
{
  public string input = "String Test";

  void Start ()
  {
    byte[] array = Encoding.ASCII.GetBytes(input);

    foreach (byte element in array)
      Debug.Log(element + " = " + (char)element);
  }
}

Keep in mind that Unity just uses Mono, which itself uses a subset of C#. If you can’t find a particular solution by googling “unity my problem”, search for “C# my problem”. Your solution was in the top 5 results of “C# cut string into bytes”, taken from this page.

1 Like

Byte.TryParse( string input, out Byte byte)
This is propably what you are looking for

1 Like

Thanks this is what I needed. For some reason I tried this before but it didnt work. Thanks again!