- Home /
Comparing two Strings. Returns equal and not equal at the same time?
Ive been banging my head on this one for 2 days now. This method searches through my object pool and 'trys' to compare strings with the passed parameter 'effect'. There are two if statements in the for loop that do the exactly the same thing and that's just me trying to get one of them to work and both of them log twice in the console like they are both equal and not-equal. i just need to compare one of the other not both . Do i need a return; some where? Do i need a break;? nothing ive tried seems to work. Thanks
public void CheckForStatusEffectInPool(Item effect, Transform recipient)
{
Debug.Log(effect.name);
statusEffect = effect;
newRecipient = recipient;
statusEffectPool = statusEffectPoolParent.GetComponentsInChildren<Transform>();
for (int i = 0; i < statusEffectPool.Length; i++)
{
if (statusEffectPool[i].gameObject.name == statusEffect.prefab.gameObject.name)
{
Debug.Log("Hello ");
statusEffectPool[i].transform.parent = newRecipient.root;
statusEffectPool[i].transform.Find("StatusEffect").gameObject.SetActive(true);
// break;
}
if (string.Equals(statusEffectPool[i].name, effect.name))
{
Debug.Log("Fixed ");
}
else
{
Debug.Log("Broken ");
}
}
}
Ive tried the if-else
if(string.Equals(string a, string b){
Debug.Log("Equals");
}else{
Debug.Log("Not Equals"); } `
and both fire in the console
Since it can't be happening on the same iteration, have you tried checking those?
if (string.Equals(statusEffectPool[i].name, effect.name))
Debug.Log($"Fixed {k}");
else
Debug.Log($"Broken {k}");
@TreyH Agreed. The "Fixed" and "Broken" lines are being output for different values of i and therefore different strings are being compared. In the terms of the question, it's not "at the same time". I can't see what the problem is.
If the OP is confused about something here, they should add more logging. For example at line 31 I would add something along the lines of
Debug.LogFormat("{0} - '{1}'=='{2}': {3}",
i,
statusEffectPool[i].name,
effect.name,
string.Equals(statusEffectPool[i].name, effect.name));
And something similar after the other comparison.
And turn off "Collapse" in the console, of course.
When the method is called, it only iterates through it once via a trigger and tries to compare the sting of the game object in my pool transform and the string of the object from the pass through parameter 'effect.name'. This is a read out when the method is called.
there should only be a log of "FIXED" or "Broken" not both. thanks for the help
Answer by GrayLightGames · Oct 23, 2019 at 07:16 PM
There are a couple of things you should try to get more information about why this is happening. My guess is that statusEffectPool has multiple elements when this runs, because the if/else should be exclusive. Try the following:
Add a Debug.Log just inside the for loop when it begins and log the value of i.
Just above the if statement in question, add a Debug.Log statement that logs out the names that are being compared.
You can Debug.Log just about any simple value, so if your code is following unanticipated routes, logging the values will help you understand why the code took a particular turn. Hope that helps, let us know what you find out!
I did a little reading on GetComponentsInChildren... it also searches the parent for a Transform component. So it's returning the child and parent transform, giving you two objects in your list. You can verify this with the Debug statements. https://docs.unity3d.com/ScriptReference/Component.GetComponentsInChildren.html
Answer by ewertonmarschalk · Oct 23, 2019 at 07:10 PM
you can use a Else If for the second if. This way, if the first one is true, it will run the code inside and jump to the end of the entire if statement, if the first if is false, it will try the else if, if its true, it runs the code inside and jump to the end, if its false it will run the else and jump to the end
if (statusEffectPool[i].gameObject.name == statusEffect.prefab.gameObject.name) {
Debug.Log("Hello ");
statusEffectPool[i].transform.parent = newRecipient.root;
statusEffectPool[i].transform.Find("StatusEffect").gameObject.SetActive(true);
} else if (string.Equals(statusEffectPool[i].name, effect.name)) {
Debug.Log("Fixed ");
} else {
Debug.Log("Broken ");
}
I think the OP is saying that the second if statement and its else are both evaluating to true, which should be impossible. The first if is irrelevant if I read the issue correct.
yes you are correct. there is only one game object in the pool that i am trying to compare strings too. Basically if the game object exist in the transform... do something... if it does not exist do something else. i am trying to create an object pooling system that will check to see if the object already exists in the pool and if it does it will be used. if it doesn't the object needed will be created with instantiate and added to the pool for later use. What its doing now is comparing the strings as both true when it iterates through the loop which should be impossible. im missing something stupid or blatantly obvious as it always turns out to be the reason.
Answer by liimbix · Oct 24, 2019 at 04:11 PM
I understand things better if I know exactly what the program is doing, but I saw this line:
if (string.Equals(statusEffectPool[i].name, effect.name))
and couldn't help but wonder why you're not doing:
if (statusEffectPool[i].name == effect.name))
Maybe string.equals is doing something funky with your code? You can't go wrong with good old conditional operators (==,!=,etc)