- Home /
the name "whichnote" does not exist in it's current context
Okay, so i'm getting the error the name "whichnote" does not exist in the current context on all of the if (whichnote [notemark] == 1-5) lines and I have no idea how to fix it. VERY beginner here by the way.
float number = Random.Range(1, 5);
if (whichnote [noteMark] == 1)
{
xPos = -0.423f;
}
if (whichnote [noteMark] == 2)
{
xPos = -0.225f;
}
if (whichnote [noteMark] == 3)
{
xPos = -0.009f;
}
if (whichnote [noteMark] == 4)
{
xPos = 0.2054f;
}
if (whichnote [noteMark] == 5)
{
xPos = 0.4119f;
}
noteMark += 1;
timerReset = "y";
Instantiate (noteObj, new Vector3 (xPos, 1.817911f, -6.038626f), noteObj.rotation);
}
On the basis of both tbe error message and the code snippet you've posted, "whichnote" has not been declared anywhere...
Answer by UnityCoach · Jul 22, 2017 at 11:35 PM
this means that the variable "whichnote" doesn't exist in the current scope.
Maybe you defined it in a method and call it from a different method. In which case, you must declare it at the root of the class.
By the way, you should rather use a switch
statement rather than several if
public class YourScript : MonoBehaviour
{
int[] whichnote;
int noteMark;
public void YourMethod ()
{
switch (whichnote [noteMark])
{
case : 1
// do this
break;
case : 2
// do that
break;
case : 3
// end so on
break;
}
}
}
I also realised that you seem to use the array only to set a position. In which case, you could use a Dictionary.
Dictionary<int, float> positions;
void Awake ()
{
positions = new Dictionary<int, float> ();
positions.Add (1, -0.423f);
positions.Add (2, -0.225f);
positions.Add (3, -0.009f);
positions.Add (4, 0.2054f);
positions.Add (5, 0.4119f);
}
void Your$$anonymous$$ethod ()
{
xPos = positions[whichnote [note$$anonymous$$ark]];
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612130909im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
My Script Wont work? 2 Answers
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System. 0 Answers