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

Help with strings

Discussion in 'Scripting' started by dezert99, Jan 3, 2015.

  1. dezert99

    dezert99

    Joined:
    Nov 15, 2014
    Posts:
    11
    Hello guys I'm running into an issue. I have a string that I want to lose a letter if backspace is pressed. Here is my code.
    if(Input.GetKeyDown (KeyCode.Backspace)){
    stringToEdit= stringToEdit.Substring(0,stringToEdit.Length-1);
    Debug.Log ("Backspace is pressed");
    }
    This should work and I know it has something to do with the stringToEdit.Lenght. If i put in my own value for the length it would work just fine. Does anyone know why this is happening?
    Incase you need to see the entire code here it is.
    using UnityEngine;
    using System.Collections;

    public class Gui : MonoBehaviour {
    public string stringToEdit = "";
    public GameManager GM=null;
    void OnGUI() {
    GUI.Label(new Rect(10, 10, 100, 20), stringToEdit);
    }
    void Update(){
    if(GM.Lockin){
    stringToEdit = stringToEdit + Input.inputString;
    Debug.Log (stringToEdit);
    }
    if(Input.GetKeyDown (KeyCode.Backspace)){
    stringToEdit= stringToEdit.Substring(0,stringToEdit.Length-1);
    Debug.Log ("Backspace is pressed");
    }



    }
    }

    Oh and also im running into a problem where when im checking the value of what the user entered in stringToEdit it wont see the answer as correct if the user enters lets say bob. But if i hard code the value for stringToEdit as bob it will work jut fine. Any ideas on why that's happening as well?
    void OnTriggerStay(Collider other){
    Debug.Log (element+"User is in trigger");
    if (mygui.stringToEdit == answer) {
    answer.text = "Correct!";
    Debug.Log ("Correct");
    }

    }
     
  2. Ruekaka

    Ruekaka

    Joined:
    Sep 12, 2014
    Posts:
    119
    I can't test it at the moment, but a first guess:

    From my understanding you add the backspace to the string, too so I would try the following:

    Code (CSharp):
    1. stringToEdit = stringToEdit.Substring(0, stringToEdit.Length - 2);

    If you check the input for "bob", what do you see?
     
  3. dezert99

    dezert99

    Joined:
    Nov 15, 2014
    Posts:
    11
    Thank you so much! That solved the first problem. What do you mean for the second?
     
  4. dezert99

    dezert99

    Joined:
    Nov 15, 2014
    Posts:
    11
    Anyone else know why the second part might be happening
     
  5. Ruekaka

    Ruekaka

    Joined:
    Sep 12, 2014
    Posts:
    119
    You said
    The question is: If the user enters "bob" what is the value you see (e.g. with Debug.Log)?
     
  6. dezert99

    dezert99

    Joined:
    Nov 15, 2014
    Posts:
    11
    Oh I see. And it will say bob In the console. That's what confuses me because it's the same exact thing as the answer but it won't display correct if the user enters it
     
  7. Ruekaka

    Ruekaka

    Joined:
    Sep 12, 2014
    Posts:
    119
    Again, what is the incorrect value you see on the screen (I guess you referring to GUI.Label)?
     
  8. dezert99

    dezert99

    Joined:
    Nov 15, 2014
    Posts:
    11
    Yes I am referring to a label
     
  9. Ruekaka

    Ruekaka

    Joined:
    Sep 12, 2014
    Posts:
    119
    o.k. and what do you see?
     
  10. dezert99

    dezert99

    Joined:
    Nov 15, 2014
    Posts:
    11
    I see bob on the label
     
  11. Ruekaka

    Ruekaka

    Joined:
    Sep 12, 2014
    Posts:
    119
    mmhh, now I'm a bit lost.
    Can you write the text and the length of answer to the console, and the same with mygui.stringToEdit and maybe for GUI.Label, too.
    Can you see any differences?
     
  12. dezert99

    dezert99

    Joined:
    Nov 15, 2014
    Posts:
    11
    Oh my gosh I just got it. The enter key was also being counted as input for the text so it had an extra space in the length. I tried everything to try and substring i kept getting length issues but then i decided to just make a mouse click be used to start the text input. Thank you for all your help!