- Home /
Animation event key callback string parameter fails equals check
I encountered a very weird behavior when using an Animations' event key with a callback and a string parameter. I have an animation with an event key that has a callback AlertEvent(string)
, passing the parameter "Anim_End"
. Inside the callback script I have the following:
public void AlertEvent(string message) {
if(String.Equals("Anim_End", message)) {
// do something
}
}
The weird behavior is that it never enters the if()
condition. I tested with multiple string equality methods like "Anim_End".Equals(message)
or string.Equals("Anim_End", message)
but none seemed to work. When I tested with locally declared string variables inside the same script, equality check worked fine so there is nothing wrong with my C# API usage. What exactly is going on here?
What does Debug.Log($"'{message}'");
output before the if
?
I tested and it did output "Anim_End" l. Do you think maybe the event handler argument is a C string instead of a string class and the debug log auto converts it?
C-strings are not a thing in C# AFAIK.
My best guess is that you have an invisible character somewhere.
Can you try typing again the string in the animation event in in your code?
Additionally, you could compare each characters or at least compare the lengths (just to figure out if there are actually invisible characters)
DebugString("Anim_End", message);
if(String.Equals("Anim_End", message))
{
// do something
}
// ...
void DebugString(string expectedString, string message)
{
if(message.Length < expectedString.Length)
{
Debug.Log("Message is shorter than '{expectedString}'. {expectedString.Length - message.Length} missing character(s): {expectedString.Substring(message.Length)}");
}
else if(message.Length > expectedString.Length)
{
Debug.Log("Message is longer than '{expectedString}'. {message.Length - expectedString.Length} extra character(s): {message.Substring(expectedString.Length)}");
}
else if(!String.Equals(expectedString, message))
{
for(int i = 0 ; i < message.Length ; ++i)
{
if(message[i] != expectedString[i])
Debug.Log($"Character #{i} are different: {message[i]} != {expectedString[i]}");
}
}
}
I thin,k but in general this is wrong since the message object and the string local variable should not point to the same instance, except if they are interned?
A string should be comparable to anything in between " "
and you are using a string
as your parameter not an object
. Unless your not passing a string, then try using message.ToString()
to compare but I'm fairly certain you would get errors if you were not passing in a string.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to keep an animation attached to gameobject 0 Answers