Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Triqy · Oct 23, 2019 at 04:34 PM · stringfor-loopcompare

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 ");
             }
 
         }
 
 
     }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Triqy · Oct 23, 2019 at 04:38 PM 0
Share

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

avatar image TreyH · Oct 23, 2019 at 05:14 PM 0
Share

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}");


avatar image Bonfire-Boy TreyH · Oct 24, 2019 at 09:09 AM 1
Share

@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.

avatar image Triqy · Oct 23, 2019 at 06:17 PM 0
Share

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. alt text

there should only be a log of "FIXED" or "Broken" not both. thanks for the help

error.png (36.7 kB)

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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:

  1. Add a Debug.Log just inside the for loop when it begins and log the value of i.

  2. 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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GrayLightGames · Oct 24, 2019 at 05:08 AM 1
Share

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

avatar image
0

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ewertonmarschalk · Oct 23, 2019 at 07:13 PM 0
Share
 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 ");
 }
 
 
avatar image GrayLightGames ewertonmarschalk · Oct 23, 2019 at 07:20 PM 1
Share

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.

avatar image Triqy GrayLightGames · Oct 23, 2019 at 08:52 PM 0
Share

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.

Show more comments
avatar image
0

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

119 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Check if specific object exist in list or array, Best Practices? 2 Answers

Compare two time strings? 2 Answers

overlapshere wont convert its hit colliders name to a string 2 Answers

How to make a refresh rate dropdown menu? 1 Answer

For loop on text without effecting each other 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges