Search code examples
c#unity-game-engineunityscriptgameobjectunity-components

Turning off a script from a script in Unity


I need to turn off a script from another script in Unity. The script is in C# and the script I am turning off is in JS. Both scripts are attached to the same object. The error I am receiving says that there is no such thing as enabled. Any thoughts? Below is part of my code.

void Update ()
{   
    var walkScript = GameObject.FindWithTag ("Player").GetComponent ("CharacterMotor");
    if ((Input.GetKey ("p")) && (stoppedMovement == true)) {
        stoppedMovement = false;
        walkScript.enabled = true;
    } else if ((Input.GetKey ("p")) && (stoppedMovement == false)) {
        stoppedMovement = true;
        walkScript.enabled = false;
    }

The error I am recieving is as follows:

Assets/Standard Assets/Character Controllers/Sources/Scripts/MouseLook.cs(41,36): error CS1061: Type UnityEngine.Component' does not contain a definition forenabled' and no extension method enabled' of typeUnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)


Solution

  • The class Component doesn't have a property enable. Try

    Behaviour walkScript = (Behaviour)GameObject.FindWithTag ("Player").GetComponent ("CharacterMotor");
    

    Assuming it is a behaviour.