|
I've been working through the old FPS tutorial, convertine the scripts to C#. For the most part I've been able to fix up the errors I've come across without too much difficulty, however I seem to have gotten stuck on the Autowaypoint script (I've looked and tried using this as well with no luck: http://www.m2h.nl/files/js_to_c.php). Here's the original JS: And here's the C# I have: using System.Collections; using System.Collections.Generic; public class AutoWaypoint:MonoBehaviour { static AutoWaypoint[] waypoints; public ArrayList connected; static float kLineOfSightCapsuleRadius = .25f; } The errors (so far): AutoWaypoint.cs(29,32): error CS0165: Use of unassigned local variable `closest' AutoWaypoint.cs(10,29): error CS0161: `AutoWaypoint.FindClosest(UnityEngine.Vector3)': not all code paths return a value AutoWaypoint.cs(54,30): error CS1061: Type AutoWaypoint.cs(79,29): error CS0103: The name `Array' does not exist in the current context AutoWaypoint.cs(89,29): error CS0103: The name `Array' does not exist in the current context edit: I've gotten the original errors taken care of, and now (I've got the feeling this is something rather simple I'm just not seeing as early as it is here :P) I'm getting: AutoWaypoint.cs(109,28): error CS0029: Cannot implicitly convert type I've updated the C# code above.
(comments are locked)
|
|
First off, you may want to use a simple array for the waypoints and connected values. They're faster, and strongly typed to boot. So, at the top- Then, whenever you would use that variable, make sure that you call the correct functions for an inbuilt array instead of an ArrayList. Errors- 1: In this code, the variable 'closest' isn't guaranteed to be assigned to everything. It is possible, if the array of waypoints has no members, for closest to never get assigned. You should fix this by returning null if the array is length zero, and then assigning current to the first waypoint in 'waypoints'. 2: This is as simple as a misplaced bracket. You should be putting right at the end of the function, not inside the foreach. Look closely, you'll see it. 3: This applies to all the 'length' issues. Anywhere where it would call for 'whatever.length', you should replace with 'whatever.Length'. I know, it's weird that it's a variable that starts with a capital, but that's just how it is. 4: The first error complaining about the non-existant 'Array' class can be fixed just by simplifying those lines to 5: The second one is a little more complex, because it requires functionality which arrays do not have. First up, add the line at the top of the page. Then, change that function so that instead of it uses Then, when you would use 'connections' in that function, use 'connectionList' instead. Then, right at the end of the function, use To convert the list back into an array that you can use elsewhere. (Make sure that you are using an inbuilt array for connected, instead of the arrayLists you are using at the moment). I think that's all, for now- revise your question as more things come up! Thanks a lot. The fixes you mentioned took care of the original errors. I think I may be misunderstanding the last part of the answer though or I'm just missing something else (see edit in question).
Dec 21 '11 at 11:43 AM
tanthalas
Yes, it's because (as I said), connected should be an array of AutoWaypoints, not an ArrayList. Remember when I said you should get rid of all that ArrayList nonsense? That's still true.
Dec 21 '11 at 12:08 PM
syclamoth
Can't believe I missed that one in the code... I probably shouldn't be trying to program at 4-6AM :P Thanks again for all the help.
Dec 21 '11 at 12:12 PM
tanthalas
I do my best coding at 4AM. **j/k
Dec 21 '11 at 12:17 PM
syclamoth
there're still existing erroes Assets/Scripts/AutoWayPoint.cs(29,24): error CS0165: Use of unassigned local variable The lastones are from return null;
Apr 19 '12 at 12:12 AM
deathbringer06
(comments are locked)
|

See that last bit? You're doing it wrong.
The line
connected = connectionList.ToArray();
needs to go at the end of the function- not in the bit where the 'Add' function went! That line should have been
Then at the end (right before the closing bracket) turn the list back into an array with the aforementioned function.