Adding a script to a prefab

I was looking at the documentation and the forums, and couldnt find a straight answer.

I cant seem to add a script i made to a prefab i created. is this because i am doing something wrong or because its not possible?

If it wasn’t possible, Unity wouldn’t be very useful. :slight_smile: The straight answer is that you’re doing something wrong. I’d check the console…I don’t think Unity lets you add scripts if you have unfixed errors.

–Eric

1 Like

that’s what i assumed. i have no errors in the console, and when i try to drag my script onto my prefab, it just wont add itself. somewhat annoying.

Are you trying to drag onto prefabs in the project view? If so, that won’t work–you can drag onto prefabs in the hierarchy (and then apply changes to prefab), or else you can add scripts from the component menu.

–Eric

awesome! that was my problem and i got it working. thanks!

correct me if I’m wrong,
But if you add a script to a prefab, in the hierachy window.
The losing prefab message will popup, and the instance will become a gameobject.

I really would like to have a way to add a script to a prefab. So all instances in all scenes will be updated.
But I can’t see/find a way?

Just do GameObject → Apply Changes to Prefab. If you just want to add a script to a prefab in the project view, do Component → Scripts → whatever.

–Eric

wtf, thanks a million!
Saves me a lot of time!

You can also drag the broken/changed prefab back onto the prefab in the project window and it’ll update.

It still won’t let me add. a script to a prefab I need help

Any script you add must inherit from Monobehaviour. Like so:

public class MyClass : Monobehaviour
{
     void Start()
     {
          // Blah blah blah
     }
 
     // Etc etc
}

If that’s not the problem, idk man. Hope that helps.