Unable to declare local variable within scope when converting AI.js script to C#

Hi forumites,

I’m trying to convert the FPS tutorial .js scripts to C# - being a total programming noob, I’ve run into my first major roadblock with the AI.js script.

I’m getting this error:

Assets/EnemyPrefabs/Scripts/AI.cs(38,46): error CS0136: A local variable named curWayPoint' cannot be declared in this scope because it would give a different meaning to curWayPoint’, which is already used in a `parent’ scope to denote something else

Here’s the code block for the method that is being affected:

C#

IEnumerator  Patrol ()
	{
		AutoWayPoint curWayPoint = AutoWayPoint.FindClosest(transform.position);
		while (true) 
		{
			Vector3 waypointPosition= curWayPoint.transform.position;
			// Are we close to a waypoint? -> pick the next one!
			if (Vector3.Distance(waypointPosition, transform.position) < pickNextWaypointDistance)
				AutoWayPoint curWayPoint = PickNextWaypoint (curWayPoint); // << this is the line that the error points to

			// Attack the player and wait until
			// - player is killed
			// - player is out of sight		
			if (CanSeeTarget ())
				yield return new StartCoroutine("AttackPlayer");
		
			// Move towards our target
			MoveTowards(waypointPosition);
		
			yield return 0;
		}
	}

JavaScript original

function Patrol () {
	var curWayPoint = AutoWayPoint.FindClosest(transform.position);
	while (true) {
		var waypointPosition = curWayPoint.transform.position;
		// Are we close to a waypoint? -> pick the next one!
		if (Vector3.Distance(waypointPosition, transform.position) < pickNextWaypointDistance)
			curWayPoint = PickNextWaypoint (curWayPoint);

		// Attack the player and wait until
		// - player is killed
		// - player is out of sight		
		if (CanSeeTarget ())
			yield StartCoroutine("AttackPlayer");
		
		// Move towards our target
		MoveTowards(waypointPosition);
		
		yield;
	}
}

Any help in setting my C# code straight would be appreciated, as this is the first time I’ve worked with waypoints, let alone C#…

Thanking you all muchly.

Oh, and Happy New Year! :smile:

I’m not sure this code will do what you want, but the problem can be solved just by removing “AutoWayPoint” before curWayPoint from the line you marked.

Thanks for the quick reply. Removing the AutoWayPoint type from the problem line gives me this error instead:

Assets/EnemyPrefabs/Scripts/AI.cs(38,33): error CS0029: Cannot implicitly convert type void' to AutoWayPoint’

I got rid of the above error by assigning a type of AutoWayPoint (explicitly typing variables is the No.1 conversion issue that I’m encountering at the moment… grrrr C#) - which in turn gives me the original ‘curWayPoint’ scope error.

I’ve got a hunch that the ‘curWayPoint’ variable isn’t being cast/typed correctly to work with C# (it works flawlessly in JavaScript) - I can’t find any API references or documentation to point me to a correctly typed syntax in C#. :frowning:

By the way, AutoWayPoint refers to an error-less AutoWayPoint.cs script.

You’re declaring curWayPoint twice - once at the top of the function, and again inside an if block. Make the second instance not a declaration by removing the word AutoWayPoint and you should be good.

I’m guessing that you might be altering the wrong line or the wrong spot, so just in case:

AutoWayPoint curWayPoint = PickNextWaypoint (curWayPoint); // << this is the line that the error points to

Change it to:

curWayPoint = PickNextWaypoint (curWayPoint); // << this is the line that the error points to

Also, based on your second error there might be something else going on, what does your PickNextWaypoint method look like? Does it return an AutoWayPoint or Void?

Thanks one and all! AI.cs conversion is now done and the compiler returns no errors! \o/

You were all right, removing the AutoWayPoint from the second instance or curWayPoint got me past this roadblock.

FizixMan - thank you for your tip on the second error too. I never thought to look beyond the line that the error notice was pointing to. Yes, the PickNextWayPoint method was returning ‘void’ - switching it to ‘AutoWayPoint’ fixed the error.

Thanks again awesome folks!