- Home /
Converting js project to c# - replacing script references
I have just finished converting all my .js files to .cs and fixing the errors. The code compiles!
Now for the super tricky part, how do I replace all the script references on all my objects? If I remove the .js files I obviously get the "missing behaviour" error in the Inspector. I'm happy manually replacing the files by dragging and dropping the .cs file onto the reference in the Inspector then removing the .js file, but Unity won't let me do that. I could have sworn I had done that in the past.
Ideas? I'm a bit frazzled after a full day of conversion and a bit frustrated to get to the end and find that the script replacement is waaay more difficult than I had thought it would be.
I didn't get what you are doing. You are just importing your scripts and then changing from "Example.Cs" to "Example.Js" ?
So I had a large project of all .js files. I converted all the javascript to C# by making a copy of the .js files, rena$$anonymous$$g them .cs and doing the code conversion. So I had two sets of files, one with .js extension and one with .cs extension. Problem is, you can't just delete the .js files and expect Unity to update all your component references. In the end I found a way to get it working, via some subterfuge. Answer below.
Answer by Gavor · Nov 06, 2016 at 09:14 AM
To have Unity simply pick up the cs files and use them as the script component references in place of the old js, I had to fool it into thinking the old js file references were the cs files. After some research into how it makes a database of files and handles references, I discovered it's all in the meta files! So, the fix was:
Rename all .js.meta files to .js1.meta
Rename all .cs.meta files to .js.meta
Rename all .js1.meta files to .cs.meta
So now each .cs file has the file reference (which resides in the .meta file) from the previous .js file of the same name. When you open Unity it sees the references just uses the cs file instead of the js in all components. From a cursory look in the .meta files (and a week of testing), it seems to work 100%.
I used a bulk file renamer to rename all the files, Bulk Rename Utility.
This worked for me! Great. Thanks Gavor. Small note: To avoid breaking public references on your scripts TO OTHER SCRIPTS, you have to do all this js.meta to cs.meta conversion at once, without opening Unity in between... but I didn't find it too hard to go one script at a time and drag the script refs back in on the inspector when they became available...
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
What scripting language should I use for a multiplayer project? 1 Answer
Multiple Cars not working 1 Answer
A simple ladder in C# or Js 3 Answers
How to make 3D text appear after conversation is complete? 0 Answers