Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by monogon · Feb 27, 2014 at 02:15 PM · eventevent-handlingsubscriptionnon-static

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image monogon · Feb 27, 2014 at 02:55 PM 0
Share

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.");
 }
 
 [...]
avatar image whydoidoit · Feb 27, 2014 at 02:57 PM 1
Share

You probably need to ensure a script execution order or as Berenger says, put the subscription in Awake.

avatar image monogon · Feb 27, 2014 at 03:01 PM 0
Share

Thank you so much, both of you!! Putting the subscription into Awake solved the problem! Thanks again!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges