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 /
  • Help Room /
avatar image
0
Question by Chubzdoomer · Mar 03, 2016 at 12:35 AM · scripting beginnervariablesscriptingbasicsclassreferences

[C#] Instantiating via class references (Need help understanding)

After watching several scripting tutorials on YouTube, something I'm having a hard time grasping is when people use class references (rather than GameObject or Transform references) to instantiate objects. I realize this is a bit vague, so I'll give an example below.

Rather than instantiating prefabs this way:

 public GameObject enemy;
 
 void Start()
 {
    Instantiate(enemy, transform.position, transform.rotation);
 }

They'll instead instantiate them this way:

 public EnemyClass enemy;
 
 void Start()
 {
    Instantiate(enemy, transform.position, transform.rotation);
 }

They ultimately still drag the prefab into the public variable's Inspector slot, but I'm having a tough time understanding why exactly this works.

I've always seen scripts as nothing more than pieces of code that you attach to objects, and that are therefore totally unrelated to those objects' transforms, materials, and so on and so forth. So why, when you Instantiate with a class reference rather than a GameObject or even Transform reference, does the entire object instantiate--script, transform, and all!?

If someone could break this down for me or help me better understand why this works and why you would ever take this approach instead of the traditional GameObject or Transform variable/reference, I would greatly appreciate it!

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

1 Reply

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

Answer by phil_me_up · Mar 03, 2016 at 12:53 AM

OK, when you create a new class (in c# at least), you'll see it looks something like

 public class MyClass : Monobehavior

This means that MyClass inherits some functionality from Monobehavior. This is known as Inheritance and it's well worth reading up on it.

The inheritance model means that whilst it may look like I'm Instantiating 'MyScript', what I'm actually doing is instantiating a Monobehaviour, which for the purposes of this we'll say is the same as a gameObject. It just so happens that if you instantiate MyScript, you know that the gameobject you create will have the MyScript component attached to it.

So if Instantiating MyScript is the same as Instantiating a GameObject then why do it? Well, for a few reasons.

  1. If you have a member variable public MyScript myScript, within the editor you can only drag prefabs objects to this which have the MyScript component attached - think of this as a sort of validation to help you make sure you put the right things in the right places

  2. If you instantiate MyScript, you immediately have access to MyScript. You don't need to do something like obj.GetComponent(), which is slow as you already have that reference when you instantiated.

  3. Good practice. It's always a good idea to know what your gameobjects are and what you expect out of them!

That is a very brief overview and there is a lot more complexity which I brushed over / simplified for the sake of grasping the concepts. As I said, it's well worth reading about Inheritance. Looking at the Monobehaviour docs is a good idea too: http://docs.unity3d.com/ScriptReference/MonoBehaviour.html

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 Chubzdoomer · Mar 03, 2016 at 01:11 AM 0
Share

Great information! Thanks!

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

38 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

Related Questions

C# Unity dot syntax exercise correct solution? 1 Answer

Make enemy patrol without Nav Points? 0 Answers

Using variables from another script. 0 Answers

random spawn enemy problem 0 Answers

Question on setting a variable to click 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