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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
2
Question by dave_mm0 · Feb 07, 2013 at 04:18 AM · gameobjectclassmonobehavior

Extending MonoBehavior and GameObject?

Hello, I am fairly new to Unity and I came across a situation where I would have liked to make a class that extends GameObject, and trying to come up with a different solution, I stumbled upon quite a few interrogations.

The situation was that I wanted to make tiles on a surface, where each tile is a GameObject and each tile will know if its index on the board (i,j) and if its accessible (boolean).

My initial instinct was to extend a GameObject. I found out quickly enough that I can't. I assume this is to maintain some consistency in the unity architecture and make sure everything is only extensible by components. Is there more to it than that?

I then started making a class with a GameObject member, which I could render. I needed the Update function (so to not have to run through all my tiles one by one and call render on each one), so I extended from MonoBehaviour.

After reading through the doc a bit, I found out that a MonoBehaviour object needs a GameObject attached to it, and that each script is in fact a MonoBehavior extension. This made my new class doubly redundant, so I ended up simply adding the indices and boolean to my script, attaching it to a GameObject and making it a prefab.

Now, to create my tile and assign its index, I do something like this:

 var newTile : GameObject = Instantiate(tilePrefab, transform.position, transform.rotation) as GameObject;
 var t : Tile = newTile.GetComponent(Tile); //Tile is the script mentioned above
 t.i = i;
 t.j = j;
 t.occupied = true;

This seems kind of unnatural to me. First, to extend a GameObject I need to actually add components to it. But to be safe, I should probably verify that the new GameObject instantiated has a component called of type Tile. This makes for a call to verify, a call to get the script, and three calls to the script to initialize its values, instad of being able to do something like

 var newTile : Tile = new Tile(i,j);

is a Tile extended a GameObject. Secondly, this means that if there is no Tile component attached to the newly created GameObject, the game will shoot out an error at runtime instead of at compilation time.

So that's it. The question is: Is there an elegant solution to this that I am unaware of, or is this the Unity way of doing things?

Secondary question: If all scripts are in fact Objects derived from MonoBehaviour, is there a situation where I would want to explicitly create a class that extends MonoBehaviour?

I hope that this was all clear, I will keep an eye on this question and edit whatever is unclear.

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 Eric5h5 · Feb 07, 2013 at 04:58 AM

I would be more inclined to write a single script that contained an array representing the board, and handled instantiating the objects as necessary. I doubt you need the Update function, unless I'm misunderstanding what you mean by "so to not have to run through all my tiles one by one and call render on each one", since Unity handles the rendering anyway. Adding components to GameObjects as you describe is "the Unity way", though I use that technique for objects that should be more or less independent, rather than tiles making up a game board (in a sense they are all part of a single object). But that's a matter of preference.

If using the GetComponent method, though, there are ways to streamline that. Casting when using Instantiate is unnecessary in Unityscript, for one (it automatically returns the type of the prefab being instantiated). It seems unnecessary to create a variable to use with Instantiate if all you're using it for is GetComponent, so those two lines can be combined. You can write a function in the Tile script that initializes the i, j, and occupied variables. So:

 var tile = Instantiate (tilePrefab, transform.position, transform.rotation).GetComponent (Tile);
 if (tile) {
     tile.Initialize (i, j);
 }

As for explicitly extending MonoBehaviour, that's only necessary when using C#. In Unity 3, using getters/setters in Unityscript required an explicit class, but that's fixed in Unity 4, so there's no reason I can think of to explicitly extend MonoBehaviour when using Unityscript now.

Comment
Add comment · Show 3 · 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 dave_mm0 · Feb 07, 2013 at 02:17 PM 0
Share

I meant that by making a class that doesn't extend $$anonymous$$onoBehavior, the class would not have an Update function. Thanks for the answer, very elaborate & instructive :) I guess I forgot the "quotes" on "Unity way"

avatar image Eric5h5 · Feb 07, 2013 at 05:28 PM 0
Share

In that case, sure, there are plenty of reasons to create a class that doesn't extend $$anonymous$$onoBehaviour. If you aren't using Update, Start, etc. then there's no need.

avatar image dave_mm0 · Feb 07, 2013 at 07:08 PM 0
Share

Yeah I got that ;) they were in fact two separate questions. thx!

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

10 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

Related Questions

scaling plane gameobject to fit orthographic view iphone unity3d 0 Answers

Accessing Normal Class from another class which derives from Monobehaviour 2 Answers

Is there any analog of GameObject.Find() function? 2 Answers

Third person controller Collide with cube 2 Answers

Unity 4 with new function for setting parent of GameObject? 1 Answer


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