0

I have been working in c# to create a generic class (which is a class that takes a given type, then behaves off of that type) e.g. the List class, which lets you create a list of a certain post-defined type. The reason why I want to do this is less important, and more difficult to explain than just presenting my question. I want to be able to check a value from another class constantly, without having to write any excess code other than instantiating the class. Here is my code for the class:

using UnityEngine;
using System.Collections;

public class ValueWatch<T> : MonoBehaviour {
    public delegate void OnValueChanged();
    private OnValueChanged onValueChanged;

    T lastValue;

    public ValueWatch(T value, OnValueChanged onValueChanged) {
        this.lastValue = value;
        this.onValueChanged = onValueChanged;
    }

    public void checkValue(T value) {
        if(!lastValue.Equals(value)) {
            onValueChanged();
            lastValue = value;
        }
    }

    void Update() {
        checkValue (
            //this is where I want the value from the original class.
            );
    }
}

I want to be able to write

new ValueWatch<bool>(exampleBool, exampleCallBackMethod);

Without calling the

ValueWatch.checkValue(exampleBool);

Outside of the valueWatch class, but I am struggling to find a way to do this. I don't want to have to assign the ValueWatch to a variable, I want it to be a one line thing. I want to retrieve the exampleBool and compare it to the last one, is this possible? If my question is unclear I will fix it. I am using unity, update is called once per frame.

Austin Gibb
  • 303
  • 1
  • 4
  • 12

0 Answers0