0

I'm facing this problem: I have three Scripts. A base class (A), a derived class with a generic, (A<T> : A) and another derived class B : A<T>. So B -> A<T> -> A.

A has two delegates. One is called when a Coroutine finishes. The other is called when we click on the GameObject.

A is as follows:

public class A : MonoBehaviour {
    […]
    p ublic delegate int delegate_OnTimeElapsed();
    public delegate_OnTimeElapsed onTimeElapsed;
    public delegate void delegate_OnMouseDown(A o);
    public delegate_OnMouseDown onMouseDown;
    [...]
    public void OnMouseDown() {
        if(onMouseDown != null) onMouseDown(this);
    }
    […]
    protected IEnumerator LifeTime(){
        [...]
        if(onTimeElapsed != null) onTimeElapsed();
    }
    […]
}

Now I have a Prefab with a B Script attached that I instantiate in a Script (let's say C) that listens to the Return key press.

C is as follows:

[…]
public B prefab_Script; //Set in editor
public void Update(){
    if(Return) {
        C inst = Instantiate(prefab_Script);
        inst.onTimeElapsed += Func_A;
        inst.onMouseDown += Func_B;
    }
}

 public int Func_A(){
    Debug.Log(“Time Elapsed”);
    return 0;
}
public void Func_B(A o){
    Debug.Log(“Mouse Down”);
}

The thing is that inside LifeTime I get a correct reference to onTimeElapsed but inside OnMouseDown I see onMouseDown as null.

Any ideas? Thank you!!

Andrés
  • 1
  • 1
  • Is it null when you do the check, or null when you attempt to invoke it? – Élie May 12 '16 at 14:29
  • Is `onMouseDown` already defined within the MonoBehaviour? – Jeroen van Langen May 12 '16 at 14:30
  • 1
    Maybe you should replace: ----if(onMouseDown != null) onMouseDown();------ for------- if(onMouseDown != null) onMouseDown(this);--------- – ORParga May 12 '16 at 14:35
  • **Good Lord**. Why are you making it so complicated? It's only Unity, and only a video game. You should be using **UnityEvent** by the way. UnityEvent: http://stackoverflow.com/a/36249404/294884 – Fattie May 12 '16 at 14:52
  • hi @ORParga. in comments, use the single tick ("backwards tick") on your keyboard to make code fragments. just start and end the code with a single tick. `example` – Fattie May 12 '16 at 14:54
  • You mention prefabs. Are you aware that "links don't work" in prefabs. you can only drag a reference "inside" a prefab. it is meaningless (if you think about it) to drag a link to something in the scene. this is a common "gotchya" with Unity. – Fattie May 12 '16 at 14:55
  • @ORParga yes indeed that is how the code is implemented, I forgot to write it down. – Andrés May 12 '16 at 15:29
  • @JoeBlow I'll try `UnityEvent` this afternoon, thanks for the advise. – Andrés May 12 '16 at 15:30

1 Answers1

0

I found the solution to my problem here: https://stackoverflow.com/a/33229734

I quote:

It's allways a better way to fill all of parameters in Instantiate method e.g

Instantiate(prefab, Vector3.zero, Quaternion.Identity)

Community
  • 1
  • 1
Andrés
  • 1
  • 1