x


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

more ▼

asked Apr 02, 2013 at 11:43 AM

douglasfigueira gravatar image

douglasfigueira
13 12 16 17

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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");
more ▼

answered Apr 02, 2013 at 11:46 AM

Graham Dunnett gravatar image

Graham Dunnett ♦♦
22.4k 32 41 122

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'

Apr 02, 2013 at 11:55 AM douglasfigueira

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.

Apr 02, 2013 at 01:11 PM EliteMossy

Thank you EliteMossy!! 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....

Apr 02, 2013 at 01:42 PM douglasfigueira
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered May 11 at 02:45 PM

Kannonfodder gravatar image

Kannonfodder
0 3

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x553
x197
x95
x40
x25

asked: Apr 02, 2013 at 11:43 AM

Seen: 3053 times

Last Updated: May 11 at 02:45 PM