- Home /
is there a way to make a script prefab?
i want to make a script prefab , so that i can add all the varaible and gameobject referneces i need to fill it all out , and the drag that onto a game object that needs it . i can do that with a gameobject , the drag the script on to it make it a prefab then fill it all in . but when i drag it to a model it add its to the gameobject heiracky and not the scrips , were i would like it to go , is there a way to do this ?
If someone answers your question, please mark it as answered. Your history shows that you consistently neglect to do this. I nearly answered your other question "collision collider game object", but wont be doing so unless you go through your old, answered questions and mark them correctly.
People time the time to help. The least you can do is give them their credit.
Answer by Khada · Aug 19, 2012 at 12:17 AM
Scripts can only exist as components on a game object (or inside other scripts / as static classes etc). However you can give your exposed variables a default value, though this doesn't really work with game object references:
public float Foo = 1; //will be 1 by default in editor
public GameObject Bar1 = null; //works
public GameObject Bar2 = new GameObject(); //wont work
public GameObject Bar3 = GameObject.Find("bla"); //wont work
numberkruncher: "If you select a script, the inspector allows you to specify default values (and associate other project wide objects) for new instances of script".
(credit to numberkruncher for the above quote)
If you select a script, the inspector allows you to specify default values (and associate other project wide objects) for new instances of script. A useful trick that I stumbled upon a while back. $$anonymous$$ight be relevant, not sure...
Oh snap, you may be right. I remember seeing it but haven't used it like that before. I'll edit my answer.
thank you , just had a look at the inspector and being blunt i never noticed that and 99% chance i never would have , cheers
I agree, it is definitely a little obscure. Another really useful factoid is the little lock icon at the upper-right of the inspector. It is SO useful!
Ah, the lock is awesome for when you want to keep a camera preview up and move things around :D
Your answer
![](https://koobas.hobune.stream/wayback/20220613075851im_/https://answers.unity.com/themes/thub/images/avi.jpg)