Disable a C# script from another C# script.

Hello! have a “Press S To Start” Script (Special thanks to @Mavina) But I also need to disable a script from that script. I have played around with the enabled property and but can’t seem to get it to work. Here is what I have so far…

using UnityEngine;
using System.Collections;

public class WaitForPlayer : MonoBehaviour
{
	public GameObject waitScreen;
	public GameObject mainPlayer;
	public Component mainScript;

	// flag to determine if we are waiting for uset input to start game
	private bool waitingToStartGame = true;

	// Use this for initialization
	void Start ()
	{
		if (waitScreen != null)
		{
			waitScreen.SetActive(true);
		}
		else
		{
			waitingToStartGame = false;
			Debug.LogError("waitScreen was not set in the inspector. Please set and try again");
		}
		if (mainPlayer != null)
		{
			mainPlayer.SetActive(false);
			mainScript.enabled (false);
		}
		else
		{
			Debug.LogError("mainPlayer was not set in the inspector. Please set and try again");
		}
	}

	void Update ()
	{
		// if the waitingToStartGame is enabled and the 'S' key has been pressed
		if (waitingToStartGame && (Input.GetKeyDown(KeyCode.S)))
		{
			// set the flag to false so that will no longer be checking for input to start game
			waitingToStartGame = false;
			if (waitScreen != null)
			{
				waitScreen.SetActive(false);
			}
			if (mainPlayer != null)
			{
				mainPlayer.SetActive(true);
				mainScript.enabled (true);
			}
		}
	}
}

Thanks! :smiley:

Try this

using UnityEngine;
using System.Collections;

public class WaitForPlayer : MonoBehaviour
{
    public GameObject waitScreen;
    public GameObject mainPlayer;

    // at a minimum the type needs to be Behaviour to get access to the variable 'enabled' but best practice is to use the actual type - e.g. MainScript
    public Behaviour mainScript;

    // flag to determine if we are waiting for uset input to start game
    private bool waitingToStartGame = true;

    // Use this for initialization
    void Start ()
    {
        if (waitScreen != null)
        {
            waitScreen.SetActive(true);
        }
        else
        {
            waitingToStartGame = false;
            Debug.LogError("waitScreen was not set in the inspector. Please set and try again");
        }
        if (mainPlayer != null)
        {
            mainPlayer.SetActive(false);
            // note: mainScript can not be on the same game object as mainPlayer, if it is then place the following above mainPlayer
            if (mainScript != null)
            {
                mainScript.enabled = false;
            }
        }
        else
        {
            Debug.LogError("mainPlayer was not set in the inspector. Please set and try again");
        }
    }

    void Update ()
    {
        // if the waitingToStartGame is enabled and the 'S' key has been pressed
        if (waitingToStartGame && (Input.GetKeyDown(KeyCode.S)))
        {
            // set the flag to false so that will no longer be checking for input to start game
            waitingToStartGame = false;
            if (waitScreen != null)
            {
                waitScreen.SetActive(false);
            }
            if (mainPlayer != null)
            {
                mainPlayer.SetActive(true);
                if (mainScript != null)
                {
                    mainScript.enabled = true;
                }
            }
        }
    }
}

You should change this

public Component mainScript;

to something like this

public MainScript mainScript;

where MainScript is the actual class name.