Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by SisterKy · Dec 28, 2009 at 10:28 PM · scriptingbasicsoop

What is a Script when it's attatched to a GameObject? An Object? A Class? A Reference?

I know this question sounds weird, but what is a Script?

Normaly I would think that it's its own category. But if a Script were its own category, its own ObjectOrientedProgramming-Concept in itself, why don't you see it described in any OOP-Primer or on the Wikipedia-page ( http://en.wikipedia.org/wiki/Object-oriented_programming )?

And I get the feeling, that a Script in the ProjectPane is something different than a Script attatched to a GameObject.

In the ProjectPane the Script usually defines one class, but it isn't really a class itself, is it?
Because it also might define several classes (see this related question: http://answers.unity3d.com/questions/2006/does-unity-support-the-declaration-of-several-classes-in-one-script ) and the script can't be several classes at the same time...?
But it isn't an object either, as it has no state.

When it's attatched to a GameObject, however, it does have a state.
So that would mean it's an object, right?
Or does it have a state, really? Maybe it's only the GameObject that stores the state and the script doesn't even know about it???

Either way, what puzzles me about this is, when you search via FindObjectOfType, you will not get the Script but the GameObject the Script is attatched to. So it apears the script only adds something to an existing object instead of being one itself.
Or is this just a shortcut the FindObjectOfType-Function will automatically provide for some reason?

So maybe it is something completely different?
Can I imagine it as the sheet of paper on which the blueprint for my object is written on, wich is neither the blueprint itself, nor the object that would be produced from the blueprint, but still can be altered by drawing on it?
Or maybe like sort of a HelperObject. That can't do much by itself but only when attatched to something, while at the same time changes the functionality of the thing it got attatched to. Kind of like a Gun won't do anything by itself, but when given to a man the man becomes a gunner and the gun becomes a deadly weapon.

Thanks & Greetz, Ky.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by jashan · Dec 29, 2009 at 12:28 AM

A script attached to a GameObject is a persistent instance of a class derived from MonoBehaviour. As such, it is one of multiple components defining both the behavior (what it can do and how it does it) and state of the GameObject it is attached to.

It can be called "persistent" when it is attached to the GameObject because any public variables are serialized and their values stored persistently. That's why when you close Unity and reopen it, the values are the same as before. It's called an "instance" because it is an actual occurrence of something the class defines.

Maybe a better name than "MonoBehaviour" would be "GameObjectComponent". But, honestly, I think MonoBehaviour sounds a lot cooler. "Mono" is probably a bit misleading, even though I guess it does make sense if you understand how the engine is built. "Behaviour" is also a bit misleading because a script does not only define behavior but also state (in fact, it does make a lot of sense to have MonoBehaviours which only define state in certain circumstances).

Anyways: Eventually, any second thoughts will fade and you'll start to love those MonoBehaviours like we all do ;-) ... in the end, those scripts that are attached to game objects are a significant part of the matter that our dreams are made of ;-) ... trust me ;-) ... it's all good ;-) ... you are being ... assimilated ;-)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SisterKy · Dec 29, 2009 at 10:37 PM 0
Share

Ah, just now I make the connection with this http://forum.unity3d.com/viewtopic.php?t=8764☆t=10 forum-post you mentioned in another answer of yours. So the "Component"-Class gives you powerful tools to extend the abilitys of an already existing GameObject but at the price of cripling itself, so no Component can live by itself anylonger (without producing errors). So it's kind of an InstanceComponentObject as oposed to a full-fledged InstanceObject. That also explains why no single Component-SubClass has a Constructor.

avatar image
1

Answer by Bampf · Dec 29, 2009 at 12:18 AM

In Unity terms, the script defines a component of a GameObject. Until it is attached to a GameObject, no instance of it exists. However, it is a class; just a class that is designed to be attached to a GameObject.

Another way to think about it is, the class represents modular behavior that you are adding to a GameObject.

Note that static methods of the class can be called even before an instance of the class exists.

(Note that I'm talking here of the typical script file, which defines a subclass of MonoBehavior. You can include scripts in your project that define classes that are not MonoBehaviors. These would not be attached to GameObjects, but might define helper classes, for instance.)

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SisterKy · Dec 29, 2009 at 10:37 PM 0
Share

Wait a $$anonymous$$oment... A non-attatched Script in the ProjectPane IS a class, HAS a class or DEFINIES a class? I would have thought a "class" to be too abstact a thing to be equal to a script...?

avatar image SisterKy · Dec 29, 2009 at 10:38 PM 0
Share

Also, does it have to be a $$anonymous$$onoBehaviour-subclass to be attatched to a GameObject, really? Or wouldn't a Component-subclass be enough? (Of course we want the functionality of the $$anonymous$$onoBehaviour-Class; I'm just asking in theory)

avatar image Bampf · Dec 30, 2009 at 02:44 PM 0
Share

True, the script file is just a bunch of code. The project pane is showing you the scripts that it sees, and therefore are compiled in the project. A script may define a class. If so, instances of the class need to be created somehow. If the class is a $$anonymous$$onoBehavior you can drag the script over to a GameObject, which creates an instance of the class (at design-time) and attaches it to the parent GameObject.

avatar image Bampf · Dec 30, 2009 at 02:57 PM 0
Share

Interesting question about Component versus $$anonymous$$onoBehavior. According to the docs, Component is all that is needed to attach to a GameObject. So theoretically you could declare a component that doesn't have behaviors like responding to mouse clicks. This would be how most Unity built-in components work. Whether the editor lets you actually do this with your own components, I don't know. (In JavaScript, the $$anonymous$$onoBehavior is implicit in the script so you don't even have the option, AFAI$$anonymous$$.)

avatar image SisterKy · Jan 01, 2010 at 04:20 PM 0
Share

Huh? I thought it was implicit unless defined otherwise? I was under the impression the "class"-keyword worked for UnityScript, too? Otherwise things like "Scriptable Object" wouldn't be accessible by UnityScript-Users?

Show more comments
avatar image
0

Answer by DaqulaZhang · Dec 30, 2009 at 03:33 AM

My question about script, if I want to make callback mechanism, I need hold one hashtable which contains string NAME and refer to each script instance I want to put in, can I use MonoBehaviour as store type? then after I got the content by the key NAME, I use it as point in C++, just like this (MonoBehaviour)(hashtable.get(key)).SendMessage(...)?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bampf · Dec 30, 2009 at 02:59 PM 0
Share

Posting an answer is not a good way to ask a question. Either post a comment, or post a new question.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Does Unity support the declaration of several classes in one script? 2 Answers

Fundamental difference between Class and Object ? 2 Answers

Does a Constructor *always* build an Object? 2 Answers

Not every Class in the API is derived from the "Object"-Class, so what are they? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges