- Home /
String comparison weirdness.
Howdy!
So, I'm building a game which tests ESL students by asking them to type the names of fruits written on virtual placards. And I'm attempting to implement a system where they can type something, have it appear on screen, and if it's the correct spelling, then it will automatically cause that fruit to fly off the screen. This works if the fruit's name is the first and only thing you type. But if you delete what you've typed (assuming a scenario where one makes a typing error), it doesn't register the two strings as equal. I've tested these two strings at runtime (see commented out flag print statements), and Unity says they're equal. So I'm way confused. Even though characterchecker.answer, the string that fruit is looking for in order to fly away is identical to typed_string, it's not registering them as equal.
Perhaps there's something subtle about strings I don't understand? I'll give a more accurate description of the error, and show you the classes where i am pretty darn sure the error lies.
I'd be much obliged and sing praises of your heroic deeds to my descendants, if you could help me out on this one. Thanks so much for your time!
--Simon
ERROR: WHEN INPUT_STRING ONCE AGAIN
SCRIPT: CHARACTER DESTROYER. THIS SCRIPT COMPARES THE NAMES OF ALL THE FRUITS ON SCREEN, THEIR VARIABLE CALLED MYTARGET.ANSWER, TO THE STRING OF CHARACTERS THAT THE USER HAS TYPED IN, CALLED TYPED_STRING. I'VE EMBOLDENED WHERE IT CHECKS FOR THIS EQUALITY.
private var mytarget; var characterchecker; var perspectivescript; var mydistance; var placards_onscreen = 1; var time_between_waves = 2.00; private var placards_active = placards_onscreen; var musicplacer: GameObject; var placardplacer: GameObject; var callednewplacards = false; var combocount = 0; var mistake_count = 0; var eyeofbeholder: GameObject; var guiscript; guiscript = eyeofbeholder.GetComponent("Basic GUI"); var numberdestroyed = 0; var missed_answer = "";
function Start() { CallNewPlacards(); }
function Update() { //print("This Frame's Variables:"); //print("-----------------------"); //print("Typed String: " + guiscript.typed_string); //print("Initial String: " + guiscript.initial_string); //print("Input String: " + Input.inputString); if(Input.GetKeyDown("backspace")) { guiscript.typed_string = ""; } if(Input.anyKeyDown && GameObject.FindGameObjectsWithTag("Ground Placards").length) { eyeofbeholder.SendMessage("AddCharToString", Input.inputString); TestClosestPlacard(); ResetPlacardTags(); } placards_active = GameObject.FindGameObjectsWithTag("Ground Placards").length; if(!placards_active && !callednewplacards) { Invoke("CallNewPlacards", time_between_waves); callednewplacards = true; } }
function TestClosestPlacard() { mytarget = FindClosestPlacard(); if(mytarget != null) { characterchecker = mytarget.GetComponent("Character Checker"); perspectivescript = mytarget.GetComponent("Perspective Cheater"); if(guiscript.typed_string != characterchecker.answer && mydistance<1600) { mytarget.tag = "Already Looked"; TestClosestPlacard(); } print("Typed:" + guiscript.typed_string); print("Answer:" + characterchecker.answer); print("can knock down:" + perspectivescript.canknockdown); if((characterchecker.answer == guiscript.typed_string) && perspectivescript.canknockdown && !characterchecker.destroyit) { characterchecker.destroyit = true; mytarget.tag = "Destroyed Placards"; combocount++; guiscript.typed_string = ""; numberdestroyed++; }
}
}
// Find the name of the closest enemy function FindClosestPlacard () : GameObject { // Find all game objects with tag Enemy var gos : GameObject[]; gos = GameObject.FindGameObjectsWithTag("Ground Placards"); var closest : GameObject; mydistance = Mathf.Infinity; var myposition = transform.position; // Iterate through them and find the closest one for (var go : GameObject in gos)
{ var diff = (go.transform.position - myposition); var curDistance = diff.sqrMagnitude; if (curDistance < mydistance) { closest = go; mydistance = curDistance; } } return closest;
}
function ResetPlacardTags() { var allplacards = GameObject.FindGameObjectsWithTag("Already Looked"); for (var placard: GameObject in allplacards) { placard.tag = "Ground Placards"; } }
function CallNewPlacards() { for(var i: int = 0; i < placards_onscreen; i++) { placardplacer.SendMessage ("SetNewPlacard"); } placards_active = placards_onscreen; callednewplacards = false; }
Answer by Catlard · May 27, 2011 at 01:01 AM
Shoot. I'm going to answer my own question. I deleted the string, and then tried to add a character to it with that sendmessage.
Does this mean I get a darwin award? Or whatever award they give to the teddy bear sitting at the booth where you can explain your bug to it?
Thanks, Unity Answers! You're my teddy bear.
Answer by testure · May 27, 2011 at 02:29 AM
In the future, use the code tags when pasting in blocks of code- nobody is going to read that mess :)
Answer by Catlard · May 27, 2011 at 02:50 AM
Ok. Thanks. I wasn't sure they had any here. I'll look it up.
Be nice, he's obviously new, so welcome! To wrap your code in tags select it and press the button that says 101 on top and 010 underneath that.
Ok, cheeers, I didn't know the difference between the two. Will do that in the future. Thanks for being friendly, everybody!
Your answer
Follow this Question
Related Questions
How do I find which string comes first alphabetically? 1 Answer
object.ToString never equals the string 2 Answers
Sort Multiple Arrays By Their Length 1 Answer
Find the differences between two lists 1 Answer
error CS0019: Operator `<=' cannot be applied to operands of type `UnityEngine.Vector3' and `float' 1 Answer