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 /
avatar image
0
Question by douglasfigueira · Apr 02, 2013 at 11:43 AM · tagif-statementsfindgameobjectswithtagif-elsecs0103

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

I'm using a code that is working fine to change the wing's color of a butterfly, through texture's offset value. Here is the code:

 using UnityEngine;
 using System.Collections;
 
 public class ButterflyColor : MonoBehaviour {     
             
     public void SetButterflyColorValue(string myButterflyVal)
     {
                 
         if (myButterflyVal == "blue") {
         renderer.material.mainTextureOffset = new Vector2(0, 0);
           }
          else if  (myButterflyVal == "orange"){
             renderer.material.mainTextureOffset = new Vector2(0.25F, 0);
         }
         else if (myButterflyVal == "yellow"){
             renderer.material.mainTextureOffset = new Vector2(0.5F, 0);
         }
         else  {
             renderer.material.mainTextureOffset = new Vector2(0.75F, 0);
         }
     }        
 }

But I need to change a bunch of wings, tagged with the tag "wings". How can I do that? I am trying to do the following code but returns the error: CS0103: The name `wing' does not exist in the current context

 using UnityEngine;
 using System.Collections;
 
 public class ButterflyColor : MonoBehaviour {
             
     public void SetButterflyColorValue(string myButterflyVal)
     {
         
         wing = GameObject.FindGameObjectsWithTag("wings");
         
         if (myButterflyVal == "blue") {
         wing.renderer.material.mainTextureOffset = new Vector2(0, 0);
           }
          else if  (myButterflyVal == "orange"){
         wing.renderer.material.mainTextureOffset = new Vector2(0.25F, 0);
         }
         else if (myButterflyVal == "yellow"){
         wing.renderer.material.mainTextureOffset = new Vector2(0.5F, 0);
         }
         else  {
         wing.renderer.material.mainTextureOffset = new Vector2(0.75F, 0);
         }
     }        
 }

Thank you ;-)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Graham-Dunnett · Apr 02, 2013 at 11:46 AM

The error you get from Unity will include two numbers inside round brackets. The first of these numbers tells you which line the error occured on. I assume you get line 9, which is where you use a variable called wing. Since this variable is used without being declared the compiler correctly complains. Wing is presumably a GameObject so you need:

 GameObject wing = GameObject.FindGameObjectsWithTag("wings");
Comment
Add comment · Show 3 · 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 douglasfigueira · Apr 02, 2013 at 11:55 AM 0
Share

Hi Graham, sorry for the lack of information... you have pointed the wright line but if I replace the line with your suggested one, I have this another error: Assets/Scripts/ButterflyColor.cs(9,19): error CS0029: Cannot implicitly convert type UnityEngine.GameObject[]' to UnityEngine.GameObject'

avatar image EliteMossy · Apr 02, 2013 at 01:11 PM 0
Share

It should be GameObject wing = GameObject.FindGameObjectWithTag("wings");

there is a difference

GameObject.FindGameObjectsWithTag("wings"); and GameObject.FindGameObjectWithTag("wings");

The first one of the above will create an array of all GameObject's with that tag, the second one will return one GameObject with that tag.

avatar image douglasfigueira · Apr 02, 2013 at 01:42 PM 0
Share

Thank you Elite$$anonymous$$ossy!! Very subtle difference that makes big difference... Now I am not having any errors, but I realize that my script is not working... before at least one wing changed color but now no one... I think I should try a for statement....

avatar image
0

Answer by Kannonfodder · May 11, 2014 at 02:45 PM

If you want to change all the wings in your current scene I would do something like this:

 Gameobject[] allWings = GameObject.FindGameObjectsWithTag("wings");
 foreach(GameObject wing in allWings)
 {
 //Wing change code
 }

I would also probably change the series of if statements to a switch statement

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

13 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

Related Questions

error CS0103 How do you fix this? 0 Answers

Using if/else statements in GUI 1 Answer

MouseLook half disabling 1 Answer

Check if there is a child with a tag? (multiple children but each diff tag) 1 Answer

OnTriggerEnter only if two tags are found? 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