I’m trying to convert all the scripts from Angry Bots into C#. I was able convert all the Explosion Scripts into C# but with General Scripts I’ve got 122 errors and I’m not sure what’s causing them. I was fairly certain I corrected all of the script’s syntaxes but apparently not. I don’t know much about Unityscript’s syntax so I could have left some Unityscript code in my C# scripts. Is there an advanced guide for converting from Unityscript to C# and is there anything I can do to improve my C# scripts based off the original Scripts in terms of efficiency?
I assume you’re just doing this for the exercise in coding, so let me make some suggestions:
Convert just one script to start with, perhaps the smallest script, and only convert that one script.
When you get that one script running (and fully tested!), then move onto the next-smallest script.
You might have more luck if you start on a smaller set of files first. Try perhaps making a scene using the Unity Standard Asset CameraFollow.js script, and then try converting that .js script to C#.
The key is to change in small steps and understand all of your changes, and make sure to reconnect all the relevant inspector public variables and TEST AFTER EACH SCRIPT that you convert.
If you introduce an error early on and don’t catch it, you’ll be very puzzled later on. And if you mis-connect (or fail to connect) an inspector variable, you’ll probably get runtime exceptions, or things just will fail to work.
Best practices specifies making what is known as a “Unit Test” in order to verify your script in one language functions identically to the script in another language.
Good luck! Make sure to take the time to understand what you’re doing and why…
Ok I’ve gotten to a point where I’m stumped. I’m getting 9 errors from PlayerAnimation saying either The best overloaded method match for UnityEngine.Mathf.Repeat(float, float)' has some invalid arguments or Argument #1’ cannot convert object' expression to type float’. So I tried converting the void to a IEnumerator but that didn’t change anything. Where exactly am I going wrong?
It’d be really helpful if, when asking about an error, you copy the line of code where the error occurs into your post. (Double-clicking the error in the Unity console should take you right to the offending line in your code.)
And no, I’m not going to download your PlayerAnimation.cs script and try it out. But you certainly can’t pass an object as either parameter to Mathf.Repeat. Check the first argument (argument #1) and make sure it is actually a number. If it’s a variable, check the declaration, and make sure it is ‘float’ rather than ‘object’.
OK, on the first one, I’m not sure, but “animationComponent(bestAnimation.clip.name)” looks very suspicious to me. This code would be valid only if you have a function called animationComponent somewhere, that takes a string parameter. Normally we capitalize function names, so I wonder about that. Where is animationComponent declared?
Also, you’ll often get that “invalid argument” error in C# if you don’t stick an “f” at the end of just about every numeric literal. You have a 2 without the f here; try making it 2f and see if it gets better. (There are exceptions — integers will generally convert to floats just fine, but it never hurts to throw in some extra f’s just in case.)
For the second one, Mathf.DeltaAngle is pretty simple: it wants two floats. So, check HorizontalAngle and make sure it’s a function that takes a Vector3, and returns a float. If it returns something else — say, a double — then you’ll run into problems, the easiest solution of which is to change its return type to float.
And oh hey, look at that, there’s the problem in snippet 3. You’ve declared HorizontalAngle to return void, even though you’re using it in places that require a float, and the body of the function does in fact attempt to return a float. So, change the return type from void to float, and Bob’s your uncle.
Cheers,
Joe
P.S. I dig what you’re doing — I think you’re going to learn a lot about both C# and Unity this way!
I figured out what the first line was complaining about. It was complaining about the fact that I didn’t change the braces into square brackets. The following below is correct.