is there a better way of dealing with scripts and prefabs instead of always manual drag and drop?

I came from programming Java from a few semesters and we’ve always been taught to program with an object oriented approach to keep everything decoupled and modular.

Something I’ve noticed watching certain guides on Youtube, quite often I see someone loading up scripts into a gameObject which they intent have having several of said gameObject in the field.

For example, in this video(

), the guy drag and drops his attack prefabs into one enemy, and then he repeats the process for another.

While it can be enough for a small game, this just seems really unpractical in the long run, especially as your game gets bigger and you’re dealing with more enemy spawns. I would imagine that ideally you would want to automate every enemy spawning with their own attack list (which can be randomly chosen from an list/array) as opposed to manually dragging and dropping.

I’m just not exactly sure how would one go about this within the Unity IDE. I’m more used to dealing everything in code and when a script is being added to a gameObject, I’m having trouble visualizing how it translate into code.

Is there an alternative or better approach to the video I posted?

Also, if there are any recommendations to good coding practices with Unity, I’d love to hear it. I’m more used to dealing strictly with code, however with gameObjects I’m completely thrown off with how scripts/methods are implemented and executed.

1 Like
gameObject.AddComponent<MyComponentClass>();
2 Likes

Since Java is not exactly my forte’, and you probably want to use Javascript (which is also /really/ not my forte), I’ll leave a few references to component system things to help you on your way.

– AddComponent Reference : Add to your object programatically. It’s references in the post above, though I don’t know if it’s JS syntax or not.

– GetComponent Reference : Get existing component and access it’s values.

The above links come in Javascript flavours. If you ever need any questions answered, chances are the API has those answers.

There is also Resources.Load(path), which you can use to Instantiate prefabs at runtime in case you wanted to work it like that.

For example, making enemies by name ref.
GameObject enemy = Instantiate(Resources.Load(“EnemyPrefab”));

1 Like

So, essentially, correct me if I am wrong, any scripts that are being dragged and dropped into a gameObject is essentially a component and can be translated in this way?

@skultrap Yes indeed it is. It works for all MonoBehaviour types. AudioClips to MovieTextures.

Another way to do it would be to make a prefab and just change the prefab. That would be the preferred method, really. You then only need to drag and drop it once, and it’ll update for all the instances (if the changes aren’t substantial or something like that, I’m not sure. It’s very iffy for me sometimes. Sometimes it updates, sometimes not).

Google prefabs and have a look-see at how to go about it, because they are the backbone of large projects and nobody works without them.

1 Like

Thanks so much for the responses!

1 Like

Of course, mate. If you’ve got any other questions about this, just drop me a PM and I’ll try and help as best I can.

In general with Unity the same coding practices apply as with all (C#) programming. Personally I mostly follow S.O.L.I.D. and for general style StyleCop. Other then that Unity is following Component based engineering. Further then that I’d ignore Unity tutorials liberal use of public member fields and make them private with the [SerializeField] attribute as they do in their latest sample projects.

Java and Javascript are not really related. C# is very similar to Java.

It can be a bit confusing to get started with Unity if you’re used to OOP.

  • component based design
  • the way scripts work and how their messages are called
  • how you create assets
  • how you assign variables in the editor

These don’t necessarily make sense from an OOP perspective at first. At least for me it didn’t. It can be confusing to find the “right” way to do things.