- Home /
Reference a JS script in a C# script?
Hi, I have a (modified) ThirdPersonController.js script that I want to reference in another script, Cheats.cs, which is coded in C#. When I try to reference the JS script (which is inside my Player) it doesn't appear like the CharacterController does, I've looked round the forums and nothing helped. It acts like the ThirdPersonController doesn't exist.
I have 2 theories to why this might be happening:
C# can not access JS scripts.
You can not reference a script, only a component.
I'm more inclined to believe in the first one. I'm happy to explain this further if you don't understand some of it.
It's true that C# cannot directly access JS, had this issue too. I've got a project, though, in which I had a JS file and put it in the Strea$$anonymous$$gAssets folder inside my Unity project, after which the JS script did still not show up as a 'suggestion' for finishing a word in $$anonymous$$onoDevelop, but could be used as a reference. Hope this helps!
This has been answered many many many times before...
http://answers.unity3d.com/questions/385582/how-to-connect-2-scripts-using-c-js.html
Answer by aman_jha · Sep 06, 2014 at 06:36 PM
Sadly, your first theory is correct. I've tried almost every possible way to connect them, but to no avail. Your best bet would be to convert the JS to C#. If it's not incredibly complex I can help. I've gotten fairly good at converting JS to C#.
Read the link tanoshimi posted. It's very possible to access JS from C#, but it's one-way (the JS script will not be able to access the C# script).
Converted to C# (my preferred language) and everything works fine! I have to agree it is easy to convert from JS to C# when you know what you're doing (as with everything). I'm actually considering converting all the base scripts into C# in case I need them later on.
Answer by Ssiroo · Sep 06, 2014 at 06:40 PM
This worked for me :
Create a folder named Plugins in the main Assets folder and put your script in there, then unity should recognise it and will allow you to access it from other scripts.
I hope it works for you too.
Good luck.
Your answer
Follow this Question
Related Questions
Why Does My GameObject Not Spawn Anymore? 2 Answers
How may I get the children (direct and dependents) of a game object? 2 Answers
Problem with Pause and Pause Menu Script 1 Answer
Distribute terrain in zones 3 Answers
How to call Variable across scripts. 2 Answers