Not sure if I understand the question

Hi! Like I said in the title. There is a question in the quiz that I am not sure to understand (maybe that’s because I am French).
It’s this one:


When we add a script to a GameObject, we add it as a component, so technically isn’t a script also a component on a GameObject?

By any chance, did you take the Complete Unity Developer course? There was a really confusing part toward the beginning, when we tried to work with text. It was confusing because there was a UI object called “Text”. Even though we were working from the Text object, we still had to grab the Text component of that object in our code. They were both called Text. We tend to think of a textbox as holding the text field that we want to edit, but in this case, the Text object had a Text component that in turn had a text field.

Similiarly, a script is just a piece of writing – a short piece of code kept in a text file on your computer. You can find all your scripts under “scripts” in your Project Window. Scripts are not a unity specific thing. They are a commonly used asset, like image files, sound files, etc. These are script files. Even if you weren’t using Unity, you might still have scripts. These are not components of Unity objects. They are your scripts and represent the files actually in your computer folder. (That’s why you can use Visual Basic or Monodevelop to edit those files. You have two programs (Unity and Monodevelop, for example) accessing the same file, so when one program makes changes, the other program sees those changes.

How does this relate to your question?

Well, if you add a script component to an object, you’ll notice there’s a field on there. You’re not allowed to edit it, because it was set for you by Unity when you wrote the name. But you’ll notice that field makes reference to a script. So just like the Text component has a text field, your script component has a reference to a script.

The script and the script component are not the same thing. It’s like if I had a sprite file, that’s different from the sprite renderer component that shows the sprite.

When you added a “new script” component to an object, Unity asked you for the scripts name and then created a script for you and linked it to this component. This is one of the behind-the-scenes things unity does for you. But that component is not your script, as evidenced by a couple of things:

  • You could select the Script from your project window, and this won’t affect the object or its components. You can move it to other folders. You can edit it with a separate file. You can do all of this without touching your objects, your components, or even being in the same scene.
  • You can add the script to other objects. Again, you can do this even in other scenes. If you go into your project folder and copy the files, you can even use these same scripts in a different project.
  • If you delete the script, the component you added to the object will still be there. (And you’ll get an error because its missing its reference script.)

I hope this helps instead of confusing you further.

Privacy & Terms