- Home /
Is there a way to reference a variable in another script if it's a different type of script?
Suppose you have most of your game created in js, but you are trying to reference a variable in a cs script. Using Find(otherObj).GetComponent(ScriptName) will automatically assume you are using the .js - how do you specify that you are using the .cs?
Actually your first notion is incorrect. Unity does nothing to assume you are referring to a js script. Often, not always, js scripts are compiled before C# scripts that are in the same compilation phase.
yes it turned out to be the compile issue - js searched before cs unless it's in the plugin directory etc
Answer by Loius · Dec 28, 2010 at 03:59 PM
If you're having a problem with compile-time errors - Try creating a folder called "plugins" or "Plugins" (not sure if capitalization is important) and putting your .cs files in there. THat'll cause them to be compiled earlier than most other scripts, and your .js files will know about their variables.
It's not that Unity assumes you're using JS, it's that Unity hasn't compiled the CS files yet so it doesn't know about them.
Answer by Berenger · Dec 28, 2010 at 02:59 PM
I've never tried it, but I think Unity translate all your scripts in something standard, so every class are registred the same way, either they are from .cs or .js, and the Find() won't care about it. That's what I think anyway.
Your answer
Follow this Question
Related Questions
Variable from a external script not being effected 2 Answers
Accessing a variable within a function.... GetComponent() 2 Answers
GetComponent with variable script possible? 1 Answer
Passing variables between classes attached to different GameObjects 2 Answers
Create a variable on a GameObject for access via the Object without GetComponent() 1 Answer