Search code examples
c#unity-game-engine3daugmented-realitygameobject

Scaling two gameobject simultaneously


I have two GameObject and basically I want both the objects to be moved together after scaling both the objects together and then move. Is it possible to change them and then move them and how to do that ? Any method will be appreciated.


Solution

  • For clarity; do you want to change the object values depending on some other objects scale? If so, when you change the scale of object, fire a C# Event and subscribe it to in class that you want to change the values. Here is sample:

    public class ScaleChangingClass
    {
        Vector3 scale;
    
        // Create event argument to pass changed scale to another class
        public class OnScaleChangedEventArgs : EventArgs
        {
            public Vector3 scale;
        }
        // create event with event args
        public static event EventHandler<OnScaleChangedEventArgs> OnScaleChanged;
    
        void ChangeScale()
        {
            // Change Scale;
            // Fire the event ?.Invoke ensure if no class subscribe, it won't throw any error
            OnScaleChanged?.Invoke(this, new OnScaleChangedEventArgs() { scale = scale });
        }
    }
    
    public class SecondClass : MonoBehaviour
    {
        private void Awake()
        {
            ScaleChangingClass.OnScaleChanged += ScaleChangingClass_OnScaleChanged;
        }
    
        private void ScaleChangingClass_OnScaleChanged(object sender, ScaleChangingClass.OnScaleChangedEventArgs e)
        {
            var changedScale = e.scale;
            // Scale changed do the thing
        }
    }