• 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 Shinzironine · Oct 30, 2013 at 12:34 PM · frameworknode

Ash Framework and Unity

I'm beginning to create a game on Unity Engine. I have a background of game development using Ash Framework on actionscript, which is as well a component based framework. In comparison with Unity, Ash supports a rather useful feature which allows to perform an update cycle only when the entity (i.e. gameObject) confroms to the specified list of present components.

Let me be more specific. For example I have a component which needs to update some internal parameter based on object visibility and on object position. The code that I come up with right now is some stuff like (in pseudo code)

 void Update() {
   VisibilityComponent vs = gameObject.get(VisibilityComponent);
   PositionComponent ps =  gameObject.get(VisibilityComponent);
 
   if (vs != null && ps != null) {
     parameter = calculate(vs, ps);
   }
 }

I think that the above code should execute only when the required components are added to gameObject. Is there any neat way of doing this on Unity? (Like in Ash)

I'm interested if it is possible to perform such filtering in Unity. Or how could I avoid null pointer exceptions during an update cycle of a component in which I have to work with some other components. The annotation [Require component] is something that I think could be handy, but it has a bit different work pattern. "Require component" creates components, but on the contrary I need to begin work (Update, Start, etc.) only when the specified components exist.

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 Tarlius · Oct 30, 2013 at 12:53 PM

You could solve the problem by using GetComponent every frame. But polling each frame would be rather inefficient, and GetComponent is rather slow too. It would probably be better to have them say they are attached in OnEnable or something.

Although I think its rather odd to attach at runtime... What are you trying to accomplish?

Have you considered setting the GameObject or MonoBehaviour inactive?

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 Shinzironine · Oct 30, 2013 at 07:04 PM 0
Share

Well, it is pretty possible that I just don't get some basics of framework. $$anonymous$$y goal is to create gameobjects at runtime with a specified set of components and then perform some actions. When I have attached a couple components, I want several of them to work only if some specified other classes of components are present. Or, let's say, if some component have been attached to a gameObject, I want others attach automatically.

To be honest, I'm just trying to understand the Unity-way of doing things. So, when I have created a couple of components, how I could ensure in some third component that these two exist? Or how is it possible to interact with gameObjects from other gameObject? Should I get others by thir name(s)? Can I get a list of all gameObjects which have a specified set of components and perform some actions on them?

avatar image Tarlius · Oct 31, 2013 at 05:36 AM 0
Share
  • $$anonymous$$y goal is to create gameobjects at runtime with a specified set of components and then perform some actions.

This is most easily accomplished via prefabs.

  • how I could ensure in some third component that these two exist?

    if(GetComponent() == null) Debug.Log("Something screwed up!");

GameObject.GetComponent will return you a reference to the component, if found. Things get a little more complicated if you have multiple instances of the same component on one object, but thats the basic idea.

  • Or how is it possible to interact with gameObjects from other gameObject?

This is mostly left up to you, but you can search all GameObjects by name, as well as search for instances of specific types of Unity.Object. Its best to maintain a list of stuff you care about yourself though, for speed. You might also want to look into the Singleton pattern, if you haven't heard of it before. Its common practice to set up manager objects as singletons.

avatar image Shinzironine · Oct 31, 2013 at 05:42 AM 0
Share

Thank you, I will look through your suggestions!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

how can use GUI.Button? framework don't recognize or suggest the method 1 Answer

Getting Unresolved Reference Warnings After Unity Update (To 5.3.4f1 64-bit) 3 Answers

Best structure for a perk system? 0 Answers

How to make a node based editor in Unity 3 Answers

Connect Unity3D app with ESP8266 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