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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by gekn1976 · May 11, 2014 at 08:48 AM · rendererif-statementsfindgameobjectswithtag

Trouble getting if statement to work

I'm trying to enable disable some gameobjects with if statements and FindGameobjectWithTag.

Needless to say I'm not doing it wright :(

Here is my code

     ///Swirl01
     public void SetSwirl (string mSwirl01) {
 
         //Default
         if (mSwirl01 == "1")
             a = true;
             b = false;
             c = false;
         
         if (mSwirl01 == "2")
             a = false;
             b = true;
             c = false;
         
         if (mSwirl01 == "3")
             a = false;
             b = false;
             c = true;
         
         
         GameObject.FindGameObjectWithTag("swirl01").renderer.enabled = a;
         GameObject.FindGameObjectWithTag("swirl02").renderer.enabled = b;
         GameObject.FindGameObjectWithTag("swirl03").renderer.enabled = c;
 
     }

I get the error "name a,b,c doesn't exist in the current context".

Can someone here please shed some light on this ??

Comment
Add comment · Show 2
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 $$anonymous$$ · May 11, 2014 at 09:00 AM 0
Share

You can use "switch":

 switch(mSwirl01)
 {
    case "1":
      a = true;
      b = false;
      c = false
      break;
    case "2":
      ...
      break;
    ...
 }
avatar image Benproductions1 · May 11, 2014 at 09:28 AM 0
Share

Even better, you could just do:

 a = mSwirl01 == "1";
 b = mSwirl01 == "2";
 c = mSwirl01 == "3";

And avoid any if statements what-so-ever. Or for even less code:

 for (int i = 1; i <= 3; i++) {
     GameObject.FindGameObjectWithTag("swirl0" + i).renderer.enabled = mSwirl01 == i.ToString();
 }

And if you want your code to be even better, don't use FindGameObjectWithTag and don't call your variables mSwirl01.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Benproductions1 · May 11, 2014 at 09:24 AM

In C#, as well as UnityScript, JavaScript, Java and basically every language that uses {} for code blocks, there are two "different" kinds of if statements:

The first one works like expected and uses {} to detonate the block of code contained in the statement. Take the following as an example:

 if (input == "lolies") {
     Debug.Log("Input was lolies");
     //count the amount of lolies
     lolies += 1;
 }

The second kind does not use {} and is criticize by many as bad practice mainly because of the exact problem you are having. The special thing about this kind, is that it only works for one line. Any other lines are not considered part of the block. Take the same code as an example:

 if (input == "lolies")
     Debug.Log("Input was lolies");
 //count the amount of inputs
 inputs += 1;

The second kind is not only annoying to maintain, since adding code to the block is impossible without adding {}, but it also makes problems like yours harder to debug and (IMO) looks ugly in general.

Just as an optimization (and to give you the compile errors you need to realize the problem), you should make your other 2 statements else if statements, since they can never be true if any of the above are.

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
avatar image
0

Answer by Kannonfodder · May 11, 2014 at 11:48 AM

BenProductions answer above istrue, but looking at your error message you also haven't declared a,b or c anywhere. You need to declare variables before you can assign values to them so either at the beginning of your function, or at the top of your document if you need to use them elsewhere you need the following :

 bool a,b,c;
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

22 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

Related Questions

Accessing GameObjects Color To Store In Variable 1 Answer

How to change the material color of an Instatiated prefab when the player enters the collision area and presses a button? 0 Answers

error CS0103 How do you fix this? 0 Answers

Changing two different objects renderer colour 1 Answer

error CS0103: The name `wing' does not exist in the current context 2 Answers


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