- Home /
"a" conflicts with a declaration in a child block
Hey everyone, I'm quite new to Unity. I saw others posting the same issue, but I cannot relate their situation to mine: where does this code go wrong? The error is the following:
Assets/Scripts/Aiming.cs(46,28): error CS0135: `xmou' conflicts with a declaration in a child block
and is related to the very last line of the following code:
public class Aiming : MonoBehaviour {
float xmou;
float zmou;
void Update () {
//1.calcolare posizione mouse ogni frame; 2.ruotare giocatore di x° rispetto al vettore (1,0,0)
//1.trasforma posizione mouse da pixel ad angolo rispetto a vettore (1,0,0); 2.per ogni quadrante aggiungi o sottrai gradi
//var pos = Camera.main.WorldToScreenPoint(transfor m.position);
float hei = Screen.height;
float wid = Screen.width;
float xmouabs = Input.mousePosition.x;
float zmouabs = Input.mousePosition.z;
if (xmouabs > wid/2){
var xmou = xmouabs - wid/2;
if (zmouabs > hei/2){
var zmou = zmouabs - hei/2;}
else{
var zmou = zmouabs;}
}
else if (xmouabs < wid/2){
var xmou = xmouabs;
if (zmouabs > hei/2){
var zmou = zmouabs - hei/2;}
else{
var zmou = zmouabs;}
}
Debug.Log (xmou.ToString ());
Answer by Paulo-Henrique025 · Oct 30, 2015 at 09:16 PM
Take a closer look, you are declaring a variable named xmou at line 3: float xmou; and inside the Update() at line 18 you declare another variable with the same name var xmou.
The first xmou has class scope and will exist everywhere inside your class, including inside any method scope, this is why you can't declare a new one. To get more information about this search for C# Scope .
Answer by Treyzania · Oct 31, 2015 at 12:40 PM
You don't need to write var or anything in front of variables when you assign them, only when you declare them.
xmou = xmouabs - wid / 2;
zmou = zmouabs - hei/2;
And so on.
Your answer
Follow this Question
Related Questions
Check if it's the first time running the game? 3 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
How do i get sprite renderer from all child. 1 Answer
Destroying childs and Instantiate [C#] 0 Answers