Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
Trigger a function when array values are changed

I am adding a set of array values through inspector window. I am trying to achieve to trigger a function, when I change my array values. That is, my script should check if the new values are not equal to old values and then call this function. I do not want to use Update as it will take more memory and processing power, so what could be the alternative?


public class SetValues : MonoBehaviour {

    [Serializable]
    public struct SetValues
    {
        public float Position;
        public float Value;
    }
 
    public SetValues[] setValues;


    void Function_ValuesChanged()
    {
        Debug.Log("The Value is changed");
        //Do Something
    }

}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Try MonoBehavior.OnValidate()

Example:

public class SetValues : MonoBehaviour {

[Serializable]
public struct SetValues
{
    public float Position;
    public float Value;
}

public SetValues[] setValues;


void OnValidate()
{
    Debug.Log("The Value is changed");
    //Do Something
}

}
over 4 years ago · Santiago Trujillo Report

0

If something happens in game and you want to notify other scripts use events. Events are lightweight and easy to implement.

In class that contain array, create property and change array value, only from property. Never directly interact with array field.

// Create Event Arguments
    public class OnArrayValueChangedEventArgs : EventArgs
    {
        public float Position;
        public float Value;
    }
    // Crate Event
    public event EventHandler<OnArrayValueChangedEventArgs> OnArrayValueChanged;
    
    Array[] myArray;
    // Change Array value only from this property, so when you change value, event will be called
    public Array[] MyArray
    {
        get { return myArray; }
        set { myArray = value; OnArrayValueChangedEvent(this, new OnArrayValueChangedEventArgs() { Position = myArray.Position, Value = myArray.Value };}
    }

From second class you should just subscribe to this event and do the thing. I will call this event from my GameManager Singleton class.

private void Awake()
{
    GameManager.Instance.OnArrayValueChanged += Instance_OnArrayValueChanged;
}

private void Instance_OnArrayValueChanged(object sender, GameManager.OnArrayValueChangedEventArgs e)
{
    // Do the thing
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!