if(transform.position.x == PrevX)
{
if(PrevX > 0)
{
int x = random.Next(-2, -4);
}
else if(PrevX < 0)
{
int x = random.Next(2, 4);
}
else if(PrevX == 0)
{
int UpDown = random.Next(1, 2);
if(UpDown == 1)
{
int x = random.Next(2, 4);
}
else if(UpDown == 2)
{
int x = random.Next(-2, -4);
}
}
}
if(transform.position.z == PrevZ)
{
if(PrevZ > 0)
{
int z = random.Next(-2, -4);
}
else if(PrevZ < 0)
{
int z = random.Next(2, 4);
}
else if(PrevZ == 0)
{
int UpDown = random.Next(1, 2);
if(UpDown == 1)
{
int z = random.Next(2, 4);
}
else if(UpDown == 2)
{
int z = random.Next(-2, -4);
}
}
}
I get an error message which says:
error CS0136: A local or parameter named ‘x’ cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
And dont get angry! I know people don’t remember what the error codes are
is the microsoft Docs page on this error. I dont know C# enough to understand what any of that really means. Could someone who knows help? Thanks.
the error means you declared the variable ‘x’ twice in overlapping scopes.
As for WHERE in your code… the error usually points to the line of code with the error. If you double click it in the unity console it’ll even open the code file at that line.
…
I notice your code declares some ‘x’ variables and sets them to a random value between some range. But never does anything with them. I also notice you didn’t show us ALL your code. So I have a suspicion that you have declared an ‘x’ before this code you showed us, and that you’re redeclaring it in these if statements. Which is causing your error.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomParkour : MonoBehaviour
{
int x = 0;
int y = 0;
int z = 0;
int PrevX = 0;
int PrevY = 0;
int PrevZ = 0;
public GameObject Platform;
System.Random random = new System.Random();
// Start is called before the first frame update
void Update()
{
int x = random.Next(-4, 4);
int y = random.Next(1, 2);
int z = random.Next(-4, 4);
int UpDown = 0;
transform.position = transform.position + new Vector3(x, y, z);
if(transform.position.x == PrevX)
{
if(PrevX > 0)
{
int x = random.Next(-2, -4);
}
else if(PrevX < 0)
{
int x = random.Next(2, 4);
}
else if(PrevX == 0)
{
int UpDown = random.Next(1, 2);
if(UpDown == 1)
{
int x = random.Next(2, 4);
}
else if(UpDown == 2)
{
int x = random.Next(-2, -4);
}
}
}
if(transform.position.z == PrevZ)
{
if(PrevZ > 0)
{
int z = random.Next(-2, -4);
}
else if(PrevZ < 0)
{
int z = random.Next(2, 4);
}
else if(PrevZ == 0)
{
int UpDown = random.Next(1, 2);
if(UpDown == 1)
{
int z = random.Next(2, 4);
}
else if(UpDown == 2)
{
int z = random.Next(-2, -4);
}
}
}
Platform.transform.position = transform.position;
GameObject duplicate = Instantiate(Platform);
transform.position = new Vector3(0, transform.position.y, 0) + new Vector3(x, y, z);
PrevX = x;
PrevY = y;
PrevZ = z;
}
}
Thats the whole code! Just in case i have made a stupid mistake!
When you stick that ‘int’ in front of ‘x’, you’re RE-declaring the variable. Don’t do that if the variable already exists and you intend to just update its value: