Author Topic: UICheckbox - Awake/Start/OnActivate Issue  (Read 7191 times)

mmuller

  • Guest
UICheckbox - Awake/Start/OnActivate Issue
« on: April 28, 2012, 04:59:09 AM »
Hi Aren,

I have a set of UICheckboxes being used to choose the music in game - everything works fine until I try setting the saved choice in awake/start. Because the behaviour of the UICheckbox is to Activate/Deactivate during initialisation.

My code saves the currently playing tune into PlayerPrefs and reads this back in Awake. I then want to set the corresponding UICheckbox to be active, however as the script itself activates/deactivates them the choice of the user gets immediately overwritten, I have seen another post from another using suggesting a 'doNotFireEventsOnStart' override and feel that this would be a very good addition to NGUI in general?

This is an extract of the code being called in Awake on the MusicController:

  1. public List<MusicChooser> MusicFiles;  
  2. public AudioSource MusicPlayer;
  3.        
  4.        
  5. void Awake ()
  6. {
  7.         int MySong;
  8.        
  9.         if (!PlayerPrefs.HasKey ("MusicPlaying")) {
  10.                 MySong = 0;
  11.         } else {
  12.                 MySong = PlayerPrefs.GetInt ("MusicPlaying");
  13.         }
  14.        
  15.         Debug.Log ("I was playing: " + MySong);
  16.                
  17.         MusicPlayer.clip = MusicFiles [MySong].MusicSelection;
  18.         MusicPlayer.Play ();
  19.                
  20.         MusicFiles[MySong].GetComponent<UICheckbox>().startsChecked = true;
  21. }
  22.  

I have tried altering the script execution order but this doesn't seem to have any effect whatsoever.

Any ideas how I can fix this from happening?

Regards,

M

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UICheckbox - Awake/Start/OnActivate Issue
« Reply #1 on: April 28, 2012, 05:03:14 AM »
Set 'isChecked', not startsChecked. I'm not sure what is activating / deactivating something.... and what that something is. Checkbox doesn't activate / deactivate anything on its own.

mmuller

  • Guest
Re: UICheckbox - Awake/Start/OnActivate Issue
« Reply #2 on: April 28, 2012, 05:16:05 AM »
Aren,

Thanks for the quick response, maybe I should post a bit more code. In the checkbox item I have a script that uses 'OnActivate' like this:

  1. public class MusicChooser : MonoBehaviour {
  2.        
  3.         // Audio Section
  4.         public MusicController MusicController;
  5.         public AudioClip MusicSelection;
  6.        
  7.         void OnActivate ()
  8.         {
  9.                 MusicController.PlayMe(this);
  10.         }
  11. }
  12.  

And my MusicController looks like this:

  1. public class MusicController : MonoBehaviour {
  2.        
  3.         public List<MusicChooser> MusicFiles;  
  4.         public AudioSource MusicPlayer;
  5.        
  6.        
  7.         void Awake ()
  8.         {
  9.                 int MySong;
  10.                
  11.                 if (!PlayerPrefs.HasKey ("MusicPlaying")) {
  12.                         MySong = 0;
  13.                 } else {
  14.                         MySong = PlayerPrefs.GetInt ("MusicPlaying");
  15.                 }
  16.                
  17.                 Debug.Log ("I was playing: " + MySong);
  18.                
  19.                 MusicPlayer.clip = MusicFiles [MySong].MusicSelection;
  20.                 MusicPlayer.Play ();
  21.                
  22.                 MusicFiles [MySong].GetComponent<UICheckbox> ().isChecked = true;
  23.         }
  24.  
  25.         // Activation
  26.         public void PlayMe (MusicChooser go)
  27.         {
  28.                 Debug.Log ("Clicked by: " + go.MusicSelection.name);
  29.                 MusicPlayer.clip = go.MusicSelection;
  30.                 MusicPlayer.Play ();
  31.                
  32.                 int MySong = MusicFiles.FindIndex (s => s == go);
  33.                 Debug.Log ("I am playing: " +MySong);
  34.                
  35.                 PlayerPrefs.SetInt("MusicPlaying", MySong);
  36.                
  37.         }
  38. }

Now when I start the menu I get this from the Debug.logs:

  1. Clicked by: Demios
  2. UnityEngine.Debug:Log(Object)
  3.  
  4. I am playing: 0
  5. UnityEngine.Debug:Log(Object)
  6.  
  7. Clicked by: Ode to tracker
  8. UnityEngine.Debug:Log(Object)
  9.  
  10. I am playing: 1
  11. UnityEngine.Debug:Log(Object)
  12.  
  13. And so on...

So it appears that the OnActivate in each checkbox is being called during the building of the menu when starting the app. Am I doing something fundamentally wrong?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UICheckbox - Awake/Start/OnActivate Issue
« Reply #3 on: April 28, 2012, 05:23:39 AM »
OnActivate has a boolean parameter. You should be checking it, and only playing your music if it's 'true'.

mmuller

  • Guest
Re: UICheckbox - Awake/Start/OnActivate Issue
« Reply #4 on: April 28, 2012, 05:27:56 AM »
Aren,

How on earth did I miss that! Well for anyone else that comes across this post the fix was as simpla as this:

  1. void OnActivate (bool isChecked)
  2. {
  3.         if (isChecked)
  4.                 MusicController.PlayMe (this);
  5. }

Many thanks for the super quick responses! And on a Saturday morning too!