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

Get a Byte from InputField

Discussion in 'Editor & General Support' started by joeyvanlierop, Sep 23, 2015.

  1. joeyvanlierop

    joeyvanlierop

    Joined:
    Sep 21, 2014
    Posts:
    27
    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.
     
  2. ketura

    ketura

    Joined:
    Oct 2, 2013
    Posts:
    29
    You're failing to get the text from the input field, or you're having trouble cutting the retrieved string into bytes?
     
  3. joeyvanlierop

    joeyvanlierop

    Joined:
    Sep 21, 2014
    Posts:
    27
    I can get the text with inputFieldName.text, but I dont know how to cut that string into bytes.
     
  4. ketura

    ketura

    Joined:
    Oct 2, 2013
    Posts:
    29
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Text;
    3.  
    4. public class test : MonoBehaviour
    5. {
    6.   public string input = "String Test";
    7.  
    8.   void Start ()
    9.   {
    10.     byte[] array = Encoding.ASCII.GetBytes(input);
    11.  
    12.     foreach (byte element in array)
    13.       Debug.Log(element + " = " + (char)element);
    14.   }
    15. }

    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.
     
    karl_jones likes this.
  5. Deleted User

    Deleted User

    Guest

    Byte.TryParse( string input, out Byte byte)
    This is propably what you are looking for
     
    karl_jones likes this.
  6. joeyvanlierop

    joeyvanlierop

    Joined:
    Sep 21, 2014
    Posts:
    27
    Thanks this is what I needed. For some reason I tried this before but it didnt work. Thanks again!