1

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?)

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Jonah Starling
  • 539
  • 6
  • 20

1 Answers1

4

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

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

Assuming it is a behaviour.

Luz
  • 494
  • 3
  • 12
  • still getting an error: Assets/Standard Assets/Character Controllers/Sources/Scripts/MouseLook.cs(38,17): error CS0246: The type or namespace name `Behavior' could not be found. Are you missing a using directive or an assembly reference? – Jonah Starling Dec 14 '14 at 08:17
  • This is weird. Try using "UnityEngine.Behaviour" instead. But it is in the same namespace as GameObject and it seems to find that one? Maybe the error message is wrong and there is something else broken. If GameObject is available Behaviour should be available too. You include booth of them with "using UnityEngine;" – Luz Dec 14 '14 at 08:30
  • Now I get this: Assets/Standard Assets/Character Controllers/Sources/Scripts/MouseLook.cs(38,39): error CS0266: Cannot implicitly convert type `UnityEngine.Component' to `UnityEngine.Behaviour'. An explicit conversion exists (are you missing a cast?) – Jonah Starling Dec 14 '14 at 08:39
  • How do I convert a component to a behaviour in Unity? – Jonah Starling Dec 14 '14 at 08:39
  • This is just basic object oriented programming. `Component walkScript = GameObject.FindWithTag ("Player").GetComponent ("CharacterMotor");` //returns a component but a component can't be activated `//a Behaviour can be activated and Behaviour inherits from Component so you can cast Component to Behaviour using this:` `Behaviour bh = (Behaviour)walkScript;` – Luz Dec 14 '14 at 08:41
  • sweet. I just casted the component to a behaviour and it worked great. – Jonah Starling Dec 14 '14 at 08:46
  • Ok. My last comment was a bit of a fail. Most Components used in unity are actually MonoBehaviour (your own code) or Behaviour but the unity methods return it as Component so you have to cast it yourself! Good look with your project – Luz Dec 14 '14 at 08:48