• 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 wojciechsura · Apr 17, 2018 at 05:59 PM · componentevent

How to listen to component field changes?

Let's say, that we have the following components:

Direction, which contains direction the object moves:

 public class Direction : MonoBehaviour
 {
     [SerializeField]
     private float angle = 0.0f;

     public float Angle
     {
         get
         {
             return angle;
         }
         set
         {
             angle = value;
         }
     }
 }

DirectionVisualizer, which visualizes this direction in form of a sphere:

 [RequireComponent(typeof(Direction))]
 public class DirectionVisualization : MonoBehaviour
 {
     private const float Radius = 2.0f;
     private GameObject sphere;

     private void PositionSphere()
     {
         Direction direction = GetComponent<Direction>();
         float angle = direction.Angle;

         Vector3 position = transform.position + new Vector3(
             Radius * Mathf.Sin(angle * Mathf.PI / 180.0f),
             0.0f,
             Radius * Mathf.Cos(angle * Mathf.PI / 180.0f));
         sphere.transform.position = position;
     }

     // Use this for initialization
     void Start () {
         sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
         sphere.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);

         PositionSphere();
     }
 
     // Update is called once per frame
     void Update () {
     
     }
 }

The relation above represents simplified model I'm having in my game - a component is used to visualize a set of parameters contained in other component(s).

Now I'd like the sphere to be automatically positioned basing on the angle field in Direction component without explicitly notifying DirectionVisualization, like:

 var direction = someObj.GetComponent<Direction>();
 direction.Angle = 25; // DirectionVisualization reacts automatically here

The simplest solution is to position sphere frame by frame, but I know, that Angle will change very infrequently and such solution would have dramatic impact on performance (in my game this visualization is a lot more complex).

I might do that by merging Direction and DirectionVisualization classes, such that I can handle Angle property setter, but that's a very ugly solution directly against SRR principle. So it's a no-go.

Other solution would be to create an event in Direction component and subscribe to it from within the DirectionVisualization component. But that raises following concerns:

  • If DirectionVisualization is destroyed, the event handler will keep it alive (I should probably detach the event handler when DirectionVisualization is destroyed?)

  • When DirectionVisualization is added to object, it should subscribe to Direction's event

  • What should happen if DirectionVisualization is added to object, which does not (yet) have Direction component? Is there a way to detect adding Direction component, such that DirectionVisualization might subscribe to the event?

  • What happens if Direction component is destroyed? How DirectionVisualization should behave? Is there way to detect it?

Is there a better way to handle notifications between components than one I proposed?

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

0 Replies

· Add your reply
  • Sort: 

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

76 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

Related Questions

Get OnMouseUp event in other object's script 1 Answer

2D Animation does not start 1 Answer

How to access other Scripts and Components 1 Answer

About animation event! 0 Answers

Animation Crossfading and Events (Legacy) not triggering at times 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