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 /
  • Help Room /
avatar image
0
Question by Unplug · Dec 26, 2016 at 05:33 PM · liststring comparison

How to compare list item to a gameobject name in C#

Hello, i'm trying to deactivate gameobject inside my GUI based on their name by comparing a list of element from an external text file to an array of GameObject. It's working when i'm hard-coding the value of the string but otherwise it's not working even though the value is good.

i can't copy paste whole code, but i'll try to put the related line

 GameObject[] myColorSwatch; //i first declare my game object array
 String tempcolor = ""; //create a var that will temporarely store TXT file content.

 void Start()
 {
     myColorSwatch = GameObject.FindGameObjectsWithTag("UI_Colorswatch"); //i populate the array on start
 }
 void HighlightText() //i create a function that is called on a mouseover even on a GUI button
         colorSourceFile = new FileInfo("./Assets/TXT/" + currentHighlightName + "c.txt"); //I declaire a text file based on button name, the text file contain 3 lines, each containing 9 number that represent a certain color RGB value, first line is 000000000
         readerColor = colorSourceFile.OpenText(); //i read the text in the text file
         tempcolor = readerColor.ReadToEnd();//store all the text in a variable
         templistcolor = tempcolor.Split('\n').ToList();//separate each line of text into separate list item
         Debug.Log("this is my tempcolor value: " + tempcolor); //return the 3 line of text
         Debug.Log("this is my templistcolor value: " + templistcolor[2]);//return the third line of text
         readerColor.Close(); //closing the reader
         //--//
         Debug.Log("this is my templistcolor number " + templistcolor[0]); //return 000000000
         string tempcolorname = "Button-"+ templistcolor[0].ToString(); //return Button-000000000
         string tempcolornumber = templistcolor[0].ToString(); //return 000000000
         string temptestcolor = "Button-"+templistcolor[0]; //return Button-000000000
         Debug.Log("this is my tempcolornumber number " + tempcolornumber);//return 000000000
         string hardcodename = "000000000";
         
         foreach (GameObject _obj in myColorSwatch)
         {
             if (_obj.name != "Button-"+tempcolornumber) //this IF statement only work if I use _obj.name == "Button-"+hardcodename) no other option work. Of course i dont want to hardcode the value...
             {
                 Debug.Log("doing the IF");
                 _obj.SetActive(false);//i want to turn every game object off but the color that exist in my text file. work when hardcoding the value in the IF statement.
             }

         }


Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Unplug · Dec 26, 2016 at 09:36 PM

I was able to find the solution and will gladly share it. Apparently, despite the Debug Log not displaying it, the string was still containing the remaining letter so the comparison in the IF statement never happen. This is non-sens. The Debug Log MUST really display everything and I see this as a bug that DEV should look into.

To get the correct behavior I added Substring to truncate my value to the desired (and predictable) lenght of 9 characther representing my RGB value inside the text file. (each line have is own color number, each color is 9 number)

         string tempcolornumber = templistcolor[0].Substring(0,9).ToString();
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

84 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

Related Questions

problem with loading list of strings on buttons 2 Answers

Deleting an object from a list of objects from another script. 1 Answer

null referance when adding to a list please help 1 Answer

add commands to execution list and execute them 1 Answer

How to Activate Random GameObjects from a list without affecting position 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