Bad operators in if statement?

I’m trying to enable and disable a script using the keyboard. I’m able to successfully turn it off (script is called “Rotate”):

using UnityEngine;
using System.Collections;

public class TurnOffRotate : MonoBehaviour {

	Rotate otherScript;

	void Start () {
		otherScript = GetComponent<Rotate>();
	}
	
	void Update () {
		if(Input.GetButtonDown("Transition"))
		{
			otherScript.enabled = false;
		}

	}
}

But my attempts to modify this to turn it on AND off aren’t working:

void Update () {

	if(Input.GetButtonDown("Transition") && otherScript.enabled == true)
	{
		otherScript.enabled = false;
	}

	if(Input.GetButtonDown("Transition") && otherScript.enabled == false)
	{
		otherScript.enabled = true;
	}
}

Yep, I’m pretty new at this.

This is because you have only the script reference and when you disable it , it is null then . Use intead the gameObject reference that has that script.

Example :

 public GameObject otherGObj;

  void Start(){
     Rotate scriptRef = otherGObj.GetComponent<Rotate>();
     if(scriptRef == null)
       Debug.Log("There is no Rotate script attached to " + otherGObj.name);


  }

  void Update(){
    if(Input.GetButtonDown("Transition")){
        Rotate script = otherGObj.GetComponent<Rotate>();

        script.enable = !script.enable;
   }
  }

You set otherScript.enabled to false, then immediately check whether it’s false and turn it back on again. Instead, you need to use “else if” for the second check. It would be better if you just checked Input.GetButtonDown once and put the rest of the logic in there. Better still would be to eliminate the if checks and just set otherScript.enabled to its opposite state. otherScript.enabled = !otherScript.enabled