- Home /
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 ;-)
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");
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'
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.
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....
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