- Home /
Can't add a script to a prefab script: JavaScript
I had a cube that had a script on it. The script cube.js accessed another script main.js. I set up a variable in cube.js
var mainScript : Main;
that allowed me to drag my Main script onto the cube.js script in the inspector. Then I had to make the cube a prefab and when I did that I couldn't drag the Main script onto the cube.js script.
Is there a work around for this? Or is there a way to just code : mainScript = main.js?
Thanks
I've problems understanding why you need to assign a script to another script variable in this way: If you want to access non-static variables from that script, you'll probably need a Game Object instance first. If you want to access static variables from $$anonymous$$ain.js, you wont need to assign that script to anything. In that case you can just use $$anonymous$$ain.. Can you give us an example of what you're doing (maybe I am just confused...)
Answer by fueldown · Jun 03, 2013 at 05:49 PM
I think I know what KieranG is trying to do. Basically AFA I understood, he is trying to drop a script in editor which is linked to a gameobject which is not a prefab, on a public script declared in a prefab script.
If this is correct, then what you are doing is not possible by just drag and drop. The reason behind this is that a prefab exists on project level and any other gameobject that is created in the editor or in script (and is not a prefab) exists on scene level. You can not assign anything scene level to anything global as that will cause it to break on scene change.
The work around I use in this case is I make both of them as prefabs. I am not sure that is efficient, but it works. After that you can drop the scripts between these prefabs.
Your answer
Follow this Question
Related Questions
Script is working incorrectly. (rotation) 1 Answer
Prefab Instancing 2 Answers
Prefabs and variables 1 Answer
How to access script variables attached to a prefab at runtime in Javascript? 2 Answers
Unity fire1 prefab in code 2 Answers