- Home /
Casting and receiving non static event from two instances problem
Hello!
I'm having trouble with non static event subscription. I will try to describe my my problem as detailed as neccessary.
In my scene there are two Player gameObjects of the class 'Player' with the names 'P1' and 'P2' and two instances of the class 'scrollviewManager' ('P1scrollview' and 'P2scrollview') which handle events from 'Player'. An array of int with 10 random values will be created onStart in the 'Player'-class. Afterwards the non static event 'arrayCreated' is casted which the class scrollviewManager has subscribed to.
// inside the player class
public delegate void PlayerEventHandler(GameObject g);
public event PlayerEventHandler arrayCreated;
public event PlayerEventHandler arrayUpdated;
[...]
for (int j = 0; j<10; j++) {
theArray [j] = Random.Range (1,5);
}
if(arrayCreated != null)
arrayCreated (gameObject);
Debug.Log (name + " Array Created.");
ScrollviewManager holds a public instance 'player' of Class 'Player' on which I drag the according player-gameObject in the inspector... so 'P1scrollview' gets 'P1' in the inspector and 'P2scrollview' vice versa.
I then subscribe to the non static event with:
// inside the scrollviewManager class
public Player player;
[...]
// Use this for initialization
void Start () {
player = player.GetComponent<Player> ();
// non static event subscription
player.arrayCreated += createArray;
player.arrayUpdated += updateArray;
}
So the scrollviewManagers are supposed to only process events from the Player-instance defined in the inspector.
The problem is I don't receive ANY events from 'P1'. 'P1' casts the Debug.Log though which states "P1 + Array created". Either 'P1' does not cast the event or 'P1scrollview' does not receive it... What am i missing?
For 'P2' and 'P2scrollview' everything works just fine. EVEN IF I disable 'P2' and 'P2scrollviewManager' in the inspector the events for 'P1' are not casting although the same scripts are attached...
Is this not the right way to use non-static events? Do I have to use different event handlers for each player instance, thus different classes for 'P1' and 'P2' ... I'd really like to avoid this if possible. I just want the player instances to cast events when each array is created and these events to be received by the according scrollviewManagers. I thought non static events where the way to go...
Thanks in advance!
Regards monogon
Answer by Berenger · Feb 27, 2014 at 02:49 PM
The lines from Player.cs you show us, where you cast the event, are they from the Start or Awake function ? I'm feeling like their is an execution order issue here.
They are from start. Here is the whole section:
public class Player : $$anonymous$$onoBehaviour {
public delegate void PlayerEventHandler(GameObject g);
public static event PlayerEventHandler rightButton;
public static event PlayerEventHandler wrongButton;
public event PlayerEventHandler arrayCreated;
public event PlayerEventHandler arrayUpdated;
// Use this for initialization
void Start () {
for (int j = 0; j<10; j++) {
theArray [j] = Random.Range (1,5);
}
if(arrayCreated != null)
arrayCreated (gameObject);
Debug.Log (name + " Array Created.");
}
[...]
You probably need to ensure a script execution order or as Berenger says, put the subscription in Awake.
Thank you so much, both of you!! Putting the subscription into Awake solved the problem! Thanks again!
Your answer
Follow this Question
Related Questions
Unity UI 4.6 - Programmatically adding events - EventTrigger.delegates is null 0 Answers
Generic Unity Event triggered from Runtime with listener added in Custom Unity Editor 0 Answers
any one now about the touch events of android swipe events on the android 1 Answer
Does MonoDevelop Autocomplete Event Listeners? 0 Answers
UI: Canvas size changed Event? 1 Answer