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 Thephil2988 · Mar 11, 2019 at 04:48 PM · gameobjectscript.inspectorgetcomponentcomponent

Get specific component on gameobject after order in inspector

If i want to get a script according its number in the inspecter, how to i go about that? thinking something along the lines of

GetComponent()

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
0

Answer by Kishotta · Mar 11, 2019 at 07:16 PM

MonoBehaviour Components don't really have any concept of what "order" they are in. The Inspector and the Unity Editor collect all of the components and display them in a somewhat predefined order, but that order and logic lives entirely outside your game and can be overridden in the Editor.

What, specifically, are you trying to do? It may be better to make references available in a prefab's inspector (through making them public or using the [SerializeField] attribute) and assign the needed references specifically.

Comment
Add comment · Show 4 · 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 Thephil2988 · Mar 14, 2019 at 11:30 AM 0
Share

In my Game i have a player who has 4 spells. Each as a scrip component attatched to the player. I want to chose one of them of random. so i was thinking if they were 1, 2, 3 and 4 i could just take a random number

avatar image Bonfire-Boy Thephil2988 · Mar 14, 2019 at 12:41 PM 0
Share

If you want randomness then you don't actually need to do what you've asked for. You don't actually care about the order so you could just use GetComponents to make an array of all the components that are Spells, and then pick one randomly from that array.

If you did care about the order then I$$anonymous$$O you'd be better off rethinking your design. For example you could give your player an array of references to spells somehow (I'd probably be looking to make the spells themselves into ScriptableObjects rather than $$anonymous$$onoBehaviours) so that "which slot a spell is in" becomes something you're in control of.

avatar image Thephil2988 Bonfire-Boy · Mar 14, 2019 at 01:21 PM 0
Share

I tried to do something along the lines of an array of script components, but when i started the game it didnt seem to like it. I attatched all the spells to a GO and at the start() did something along the lines of array[0] = this.GetComponent

avatar image Bunny83 · Mar 15, 2019 at 09:25 AM 0
Share

That's actually not true since Unity version 5.0. With that version they shipped the new audio system and the filter stack system. Those filters rely on the component order in the inspector and since this version we actually have the ability to explicitly re-order the components in the inspector.


So GetComponents does return the components of a gameobject in the order they are specified in the inspector.


However in the case of the OP (having a list of spells), it makes more sense to use some OOP (interfaces, abstract base class) and simply store all relevant instances in an array or List. $$anonymous$$eep in $$anonymous$$d that GetComponents<I$$anonymous$$yInterface>() actually works and only returns components which implements this interface.


Though for a more specific answer the question need to be more specific.

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

159 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 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 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 do I keep my sprite standing upright?, 2 Answers

Acessing a Script Instance from Another Object 3 Answers

What is the difference between GameObject.GetComponent and Component.GetComponent? 1 Answer

Accessing script variables of instantiaded prefab 1 Answer

Unable to get component from a gameobject 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