Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by GreenTee · Apr 03, 2017 at 03:05 PM · classgameobjectsinstance

Does attaching a script to a GameObject makes the GameObject an instance of the class?

Hello Com,

as the title says:"Does attaching a script to a GameObject makes the GameObject an instance of the class?"

I am asking because often we do: public class SpawnManager : MonoBehaviour {

     public GameObject gmObj;
     //..other code
 }

Now if i write a class "Player" and initiate it:

     public class SpawnManager : MonoBehaviour {
     
         public Player plObj;
         //..other code
     }

Does it mean all objects with the Player-class attached to it, become "Player"-Object instances or are these objects, objects with a player class attached to it and not more?

Comment
Add comment · Show 1
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 TreyH · Apr 03, 2017 at 03:14 PM 0
Share

Well, you haven't "initiated" it there. You've declared that Spawn$$anonymous$$anager instances will have a Player object named plObj and that this field is public. You may have noticed that $$anonymous$$onoBehaviours do not allow you to construct one using something like "new Spawn$$anonymous$$anager()".

So, the short answer is "no". To verify this immediately, try casting a GameObject as Player after attaching a Player script / component to it.

3 Replies

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

Answer by Bunny83 · Apr 03, 2017 at 03:54 PM

Attaching a script or component in general to a gameobject will create an instance of that component. However the gameobject itself doesn't "become" something else. Think of a GameObject like the chassis of a car. It's just the frame without any functionality on it's own. It just has "attachment points" where you can install other components like an engine, driver seat, ect.

Each component is it's own object and it's own instance. To get a list of all components of a specific type attached to a gameobject you can use someGameObject.GetComponents<Type>().

The Unity editor offers a quite smart drag&drop functionality. When you drag a gameobject onto a variable of a component type, it automatically checks if the gameobject has a component of that type attached. If it has, it assigns the reference to that component instead of a reference to the gameobject.

Components can't "live" on their own. They always need to be attached to a GameObject. That's why you can't create an instance of a Component manually. Only by using gameObject.AddComponent<Type>(). When you drag a script onto a gameobject, Unity essentially does exactly that. It internally creates an instance of your component and attaches it to the gameobject.

Comment
Add comment · 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
1

Answer by meat5000 · Apr 03, 2017 at 03:15 PM

As a Variable, a 'GameObject' in a script is simply an empty memory-box in the shape of a GameObject. It is a variable of that class.

However, if you Instantiate a GameObject it becomes a Game-World Instance object with an attached Transform Component.

Comment
Add comment · 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
1

Answer by NoseKills · Apr 03, 2017 at 03:35 PM

Nope. A GameObject is always just a GameObject and it only inherits UnityEngine.Object.

A GameObject can have Components attached to it. For example scripts that you write yourself inherit the MonoBehaviour class, which inherits from Behaviour class, which inherits Component class, and thus can be added to a GameObject as a Component and fetched with GetComponent(). Also for example the Transform that is a part of each GameObject inherits Component. You can reference the transform component of a GameObject either by using the property transform of the GameObject or any component on it or by calling GetComponent<Transform>().

Adding a component to a GameObject doesn't change the GameObject itself. Generally in C# the type of an instance or a variable can never change.

A GameObject can have multiple instances of the same Component type (depends on the component) or many different Components attached to it, so it doesn't make sense that the GameObject would change to one of them randomly.

The inspector fields in Unity are smartly designed so that if you drag a GameObject into a field of type X in the inspector, the inspector picks a reference to an object/Component of type X if one is found on the dragged GameObject. Perhaps this is what got you wondering?

Comment
Add comment · 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

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

69 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to convert System.Type into an instance or access the class variables 3 Answers

[SOLVED]How to use attributes from array of custom class instances? 1 Answer

Access class from all scripts 1 Answer

Scriptable object gets deleted with a class that holds it? 1 Answer

Set Dirty on class instance? 0 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