Script Component does not call OnEnable() when enabled switched to true

So I’m new to Unity, and I’m trying to get a manager component to enable and disable other components, but I’m having difficulty in actually running the other components’ code.

Here is an example snippet:

if(m_clientObject != null) {
						ClientBehaviour temp = m_clientObject.GetComponent<ClientBehaviour>();
						//ClientBehaviour temp = (ClientBehaviour)getFirstBehaviourOf(typeof(ClientBehaviour));
						if (temp != null) {
							temp.m_connectEndpoint =	sessions*.EndPoint;*
  •  					temp.m_shouldConnect = 		true;*
    
  •  					temp.enabled =				false;*
    
  •  					temp.enabled =				true;*
    
  •  					BoltLog.Info("Connecting to server...");*
    
  •  				}*
    
  •  				else*
    
  •  					PrintClientComponentError();*
    
  •  			}*
    
  •  			else*
    
  •  				PrintClientDefinedError();*
    

I was initially believing that the enable variable had a custom setter and getter that would call OnEnable() if set to true, but I was proved wrong (so I’ll have to change the above code to reflect my findings). What is the correct way to coax the other MonoBehaviour-derived component to call their OnEnable() functions?
Also, on a side note: I’ve noticed that public variables in a class do not reflect those variables in the Inspector if they have a custom setter and getter. Why is this so?
Example:

  • public string m_udpEndpoint {*
  •  set {*
    
  •  	_m_udpEndpoint =	value;*
    
  •  	m_connectEndpoint =	UdpEndPoint.Parse(value);*
    
  •  }*
    
  •  get { return _m_udpEndpoint; }*
    
  • }*

OnEnable does get called when setting the game object to active or the script to enabled.

Here is the setup I have:

public EnabledScript obj;
void Update () {
	if (Input.GetKeyDown(KeyCode.Space)){
		obj.enabled = true;
		obj.enabled = false;
	}
}

public class EnabledScript : MonoBehaviour {
    void OnEnable () {
	print ("Hey");
}
}

They are both on different objects and it prints each time I am enabling the script. Maybe you typed OnEnable wrong.

As for your property not showing, I would think

_udp_Endpoint 

is probably private and the property won’t show in the inspetcor itself since it is a method and not a variable.

All right. After getting a little more familiar with Unity I realized that I had not instantiated the components because they were sitting in a prefab, and after instantiating my ServerObject and ClientObject into the scene, I then linked the appropriate gameObjects into the NetworkBehaviour instance, and it worked perfectly.

So a little lesson on those of you new to Unity: If something’s not working, make sure it and its dependencies are instantiated in the scene.

I just posted this useful Unity 5.4 snippet to determine if your just entering play mode or leaving play mode into editor mode.

    [InitializeOnLoad]
    class MyClass
    {
        static bool state;

        static MyClass()
        {
            EditorApplication.playmodeStateChanged += PlayModeChanged;
            
        }

        static void PlayModeChanged()
        {
            if (Application.isPlaying != state)
            {
                state = Application.isPlaying;
                Debug.Log("Playmode Changed : " + Application.isPlaying);
            }
        }
    }