Scope of variables

Hi,

I just came across a quite strange issue which is not logical to me at all.

if (true) {
		var duplicate : int = 0;
	}
	else if (false) {
		var duplicate : int = 0;
	}

That code throws an error in Unity, “There is already a local variable with the name ‘duplicate’”.
Are the variable scopes in JavaScript different than in C#?
If yes, I would really be surprised

Regards

I had the same problem when I started.
the scope of the local variables seem to be function wide.
logically they should be block wide.

Welcome to the sometimes twisted world of UnityScript/JavaScript. It’s working as intended, apparently. I hate the behavior.

-Jon

That’s just odd, odd, odd…

Can anyone from OTEE point out why it is that way?

Regards

Yes, it’s very odd. It’s also the way JavaScript language standard is defined.