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
0
Question by benjamin.rickels · Apr 07, 2015 at 04:26 AM · gameobjectscript.componentclassmonobehaviour

Do you tend to have scripts to mark prefabs

Granted, the title is somewhat bad, but let's say you have a GameObject that represents your player.

The player has a 'Health' script managing its health, a 'PlayerMovement' script handling its movement and an 'Inventory' script, managing its inventory.

Do you also have a 'Player' script on your GameObject representing your player, which gives you easy access to all the other components that a player always has (so you only need a reference to this script to access all the player's other scripts)? Do you maybe even create all the other scripts in the 'Player' script's 'Awake' method via 'AddComponent' instead of dragging them onto the GameObject at edit-time?

Or do you think a component shouldn't describe the GameObject it is attached to at all; it should only add behaviour to it?

Comment
Add comment · Show 7
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 gjf · Apr 06, 2015 at 11:15 AM 0
Share

you're right, the title isn't the best ;)

this really should be in the forums, but...

unity uses a component model where each component is a separate class. try to keep functionality related to a particular type of object in its own class (script). by all means extend them though - you may have a number weapons or pickups (or both!) which share common variables/functionality - search 'inheritance'.

whether you want to add them all via code or in the unity editor is up to you... for speed of development, you may want to add them in the editor, but there's no right or wrong way.

regarding the na$$anonymous$$g, whether it's a component, method or variable name - ALWAYS use something which describes what it does. if you're struggling with a sensible name, you've probably got bigger issues! try to keep methods limited to one thing too - search 'single responsibility'.

avatar image gjf · Apr 06, 2015 at 03:30 PM 1
Share

i see. to the point in your last paragraph, yes - the Player script would get references to everything it needed on the Player prefab - that's a common approach. however, accessing everything via this class isn't necessarily the most straightforward. static variables aren't so evil as to totally avoid them, but you could be overcomplicating things...

i don't know enough about the other components on your player or any use case so it's impossible to suggest anything more specific. as you're aware, there's often more than one way to do something ;)

avatar image benjamin.rickels · Apr 06, 2015 at 03:58 PM 1
Share

Yes, that there's nearly always more than one way of doing something is indeed right ;) .. However I often try to get some opinions from others to avoid potential difficulties, I hadn't thought of before. And sometimes you even get some inspiration on how to solve other problems, too. So "overcomplicating" is certainly true, however you also learn a lot if you ask more than what would seem helpful at first.. At least my experience. But as you suggested, I also posted the question on the forum and maybe I'll get some more more people into the discussion there.

But thanks a lot for your answer again! I appreciate every form of contribution to my questions...

avatar image Baste · Apr 07, 2015 at 08:48 AM 1
Share

If you want things to follow (or avoid or in any way react to) the player character in a different way from how they react to other things, you'll have to mark it in some way.

You could tag it "Player", but string comparrisons are hard to debug and slow, so comparing the tag of every gameobject any script collides with or otherwise sees with "Player" might be something you'll want to avoid.

There's some solutions. You could subclass the components on your player object - I$$anonymous$$ have PlayerHealth : Health on your player character - and look for that when you need to react to the player. You'll probably want that anyways, since the player's health script probably needs to send messages to the UI in some way the other health instances doesn't (unless your UI is listening to a specific instance).

You'll also probably have a Game$$anonymous$$anager class or something like that which controlls the general state of your game. If that's the case, that could provide access to the player character for other scripts.

Finally, there's the proposed solution of a Player script. I would use if it does anything else than being a dummy marker. You'll probably end up having some unique player behaviour that's not a part of any general scripts anyways. In that case, using the Player script as you posted is probably the easiest way to do these things.

On the whole spawning scripts vs. adding them on Awake - using the RequireComponent is probably the safest way of ensuring that the correct scripts are in place.

avatar image Baste · Apr 08, 2015 at 06:36 AM 1
Share

I believe RequireComponent takes an arbitrary length list. So, I'd guess you're limited by the upper size of array, or 2^31-1 components.

Which should be just about enough.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Pangamini · Apr 07, 2015 at 09:56 AM

Not prefabs, but logical objects or "entities". Yes, I always have some leading or main component on the root gameObject. Then i either derive from it, or keep it sealed and just add components, which often derive from some base class of mine (like EntityComponent). Usually I will also create my own Update method called by some manager that holds all of my Entities, so it's easier to control or pause the game (without changing the engine timeScale), save the game or do whatever level related tasks that should not affect eg. the controller part (inputs, gui, camera etc). So often when i refer to the object, i don't refer to the GameObject, but this main component. That's how i diffrerentiate between actual game objects (car, mine, rocket) and supporting objects (camera, game manager, texture generator)

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 benjamin.rickels · Apr 07, 2015 at 06:51 PM 1
Share

Thank you very much Panga$$anonymous$$i!

This seems exactly what I want to do, except you could frame it way better than me. And while I'm reading your explanation I think it is very useful as well. It combines the flexibility of an Entity-Component-System with the advantages of classes being the main entities in "classic" program$$anonymous$$g. And it made me realize that having a main script for every prefab isn't the best way to go... So really thank you very, very much again! I'll just leave the question unanswered for one or two more days, for maybe there's an even better answer (though I honsestly doubt it), but I guess you can see your answer already as the accepted one ;) !

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

creating Custom class 1 Answer

Simple abstractions. Reusing code outside of class 1 Answer

How do I keep my sprite standing upright?, 2 Answers

Access the GameObject a class is attached to 1 Answer

Disabling GameObject doesn't disable components 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