1

Very simple question: In c# (in Unity), I create a new custom class from a Monobehaviour class attached to a gameobject:

CustomClass test1 = new CustomClass(t1, t2, 0.5f);

I want this class instance to know who its creator is - it's a coroutine and it should check if it's parent is destroyed. Of course I can pass it like this:

CustomClass test1 = new CustomClass(this, t1, t2, 0.5f);
///
CustomClass(MonoBehaviour creator, string t, string t2, float a);

But, is there a more elegant (=automatic) way to do this? I am aware of the following method for the static functions:

public static void Test(this MonoBehaviour behaviour, float a);
///
this.Test(0.5f);

Is there something like that for a class constructor?

Sammy S
  • 23
  • 3
  • You could add an extension method to `MonoBehavior` that calls the constructor and passes the `this` instance.. – Blorgbeard Sep 16 '17 at 01:38
  • I don't think there's an "elegant" way for a class to refer to its owner because that kind of goes against the concept of encapsulating things in classes. Maybe in the owner's destructor you could have it tell the co-routine to stop executing, or pass it to a different owner, or whatever you need to do. – smead Sep 16 '17 at 01:42

1 Answers1

0

This has to be done manually. There is really no way to pass that reference automatically but there is a method to do this without passing it through function or constructor.

You can use static dictionary to simplify this. Although, you must add the CustomClass and MonoBehaviour to the dictionary each time you create new instance of CustomClass.

In the CustomClass function, you can then have a function that retrieves the MonoBehaviour from the dictionary by passing in the CustomClass instance to that static dictionary.

Example of the class that is creating new instance of the CustomClass class:

public class TestMono: MonoBehaviour
{
    public static Dictionary<CustomClass, MonoBehaviour> classToMonoBehaviour;

    // Use this for initialization
    void Awake()
    {
        classToMonoBehaviour = new Dictionary<CustomClass, MonoBehaviour>();

        //Create and add class to the dictionary
        CustomClass cclas = new CustomClass();
        classToMonoBehaviour.Add(cclas, this);
    }
}

The CustomClass class that needs to access the MonoBehaviour that created it:

public class CustomClass
{
    public CustomClass(){}

    public MonoBehaviour getCreator()
    {
        //Access MonoBehaviour from the dictionary
        MonoBehaviour result;
        TestMono.classToMonoBehaviour.TryGetValue(this, out result);
        return result;
    }
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Thanks for the complete answer, no way - no problem. Although, I have to note that your suggestion has little sense, as it is much longer story as to define and declare and store a special dictionary than to just pass "this" as an argument. – Sammy S Sep 16 '17 at 15:45
  • Yup that's right. It's only suggested to let you know that you don't even need to pass `MonoBehaviour` to another class. Remember that declaration of the dictionary is done once only. What you do each other time is to add it to the Dictionary. – Programmer Sep 16 '17 at 17:21