Disabling Scripts

I’m making an upgrade menu, and i’m trying to disable a script during its activation. The script is shown below.


using UnityEngine;
using System.Collections;

public class PlayerStats : MonoBehaviour {

private PlayerStats stats;

public static PlayerStats instance;

public int startingHealth = 100;

[SerializeField]
private GameObject upgradeMenu;

public delegate void UpgradeMenuCallback(bool active);
public UpgradeMenuCallback onToggleUpgradeMenu;

public int _currentHealth;
public int currentHealth 
{
	get { return _currentHealth; }
	set { _currentHealth = Mathf.Clamp (value, 0, startingHealth); }
}

void Update ()
{
	if (Input.GetKeyDown (KeyCode.U)) 
	{
		ToggleUpgradeMenu();
	}
}

private void ToggleUpgradeMenu()
{
	upgradeMenu.SetActive (!upgradeMenu.activeSelf);
	onToggleUpgradeMenu.Invoke(upgradeMenu.activeSelf);
}


void onUpgradeMenuToggle ()
{
	GetComponent<PlayerMovement> ().enabled = false;
	GetComponent<PlayerShooting> ().enabled = false;
}

void Awake()
{
	if (instance == null) {
		instance = this;
	}

	currentHealth = startingHealth;
}

}


The code I wrote for the function i’m trying to use is this:


void onUpgradeMenuToggle ()

{

GetComponent<PlayerMovement> ().enabled = false;
GetComponent<PlayerShooting> ().enabled = false;

}


Is there something I missed in the whole code, or did I just do the function wrong?

If you’re using the delegate in the same script that you’re defining it - shouldn’t you be subscribing (and unsubscribing) to it?

Try adding (anywhere in the code)

void OnEnable()
{
UpgradeMenuCallBack += onUpgradeMenuToggle();
}

void OnDisable()
{
UpgradeMenuCallBack-=onUpgradeMenuToggle();
}