Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Dec 15, 2014 at 05:52 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Oatz · Dec 15, 2014 at 05:48 PM · if statementstring comparison

Unity string comparison not working...

I cant seem to get the name of the object (string) to match the string variable Roid1, in the IF statement. The name of the selected target is Roid1 when selected in game.

 public Transform selectedTarget;
 private string Roid1;
 private string Roid2;


 if(selectedTarget.name == Roid1){
     Material roidselected = Resources.Load("RoidSelected1", typeof(Material)) as Material;
     selectedTarget.renderer.material = roidselected;
     }        
 if(selectedTarget.name == Roid2){
      Material roidselected2 = Resources.Load("RoidSelected2", typeof(Material)) as Material;
      selectedTarget.renderer.material = roidselected2;
      }        








Comment
Add comment · Show 3
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 Oatz · Dec 15, 2014 at 04:57 PM 0
Share

No i didnt. Unless im misunderstanding, the reason i didnt, was so that selectedTarget.name could be compared in following IF statements and thus set different materials accordingly.

avatar image Oatz · Dec 15, 2014 at 05:11 PM 0
Share

Ima moron..

private string Roid1 = "Roid1";

Sorry :<

avatar image MrSoad · Dec 15, 2014 at 05:21 PM 0
Share

It's fine :D

You understand now and that is the important thing ;)

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by MrSoad · Dec 15, 2014 at 04:50 PM

Have you given the string "Roid1" a value(string) before this if statement, such as :

 Roid1 = "The_Name_Of_The_Target_You_Want";
Comment
Add comment · Show 2 · 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 MrSoad · Dec 15, 2014 at 04:59 PM 0
Share

Then you are comparing whatever the value(string) of this "selectedTarget.name" is to nothing, "Roid1" has no value so it will never == "selectedTarget.name".

In an If statement the condition has to be met for it to execute. In your case the two sides selectedTarget.name and Roid1 have to match exactly. How can something with no value match anything other than something else that has no value. You are misunderstanding how this code works. You must have several possible names that can be the "selectedTarget.name" so you need to check against each name :

 if(selectedTarget.name == "Name_Of_Your_First_Object") {
      $$anonymous$$aterial roidselected = Resources.Load("RoidSelected1", typeof($$anonymous$$aterial)) as $$anonymous$$aterial;
      selectedTarget.renderer.material = roidselected;
 }
     
 if(selectedTarget.name == "Name_Of_Your_Second_Object") {
       $$anonymous$$aterial roidselected2 = Resources.Load("RoidSelected2", typeof($$anonymous$$aterial)) as $$anonymous$$aterial;
       selectedTarget.renderer.material = roidselected2;
 }   

You don't even need the string vars for this. Hope this makes it clearer :)

avatar image MrSoad · Dec 15, 2014 at 05:53 PM 0
Share

Thanks @meat5000, I was making a right mess of trying to sort the order out :)

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

String comparison weirdness. 3 Answers

Why do I get this error:Expressions in a statement must be only executed by their side effects? 1 Answer

I'm getting really mixed up on if statements! Too many! Help! 2 Answers

IF statement not working? 0 Answers

InvokeRepeating inside if statement 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