0
\$\begingroup\$

I've got an issue. When (Input.GetMouseButtonUp (0), Speedy script is disabled, thus we cannot enable it again from inside the script like shown below. How can we go around this?

public class  Speedy : MonoBehaviour {

GameObject car;

 if (speed == good)
    {
        GameObject car = GameObject.FindWithTag ("Car");

        if (Input.GetMouseButtonUp (0)) 
        {
            car.GetComponent<Speedy>().enabled = false;
        }

        else car.GetComponent<Speedy>().enabled = true;
    }
}
\$\endgroup\$
9
  • \$\begingroup\$ This is more an architecture issue than a problem with an if statement. Check for your mouse click outside Speedy, and activate or deactivate it from there. \$\endgroup\$ Commented Jan 12, 2016 at 20:36
  • \$\begingroup\$ @Almo Title edited. \$\endgroup\$ Commented Jan 12, 2016 at 20:36
  • 1
    \$\begingroup\$ once you disabled script so how do you expect to run same script to enable it? which is now disable? \$\endgroup\$ Commented Jan 12, 2016 at 20:39
  • \$\begingroup\$ @HamzaHasan That's my question. Is there another way? \$\endgroup\$ Commented Jan 12, 2016 at 20:40
  • \$\begingroup\$ Control all of these operations from another class. \$\endgroup\$ Commented Jan 12, 2016 at 20:43

1 Answer 1

1
\$\begingroup\$

For this, you can use FindObjectOfType either.

FindObjectOfType gives you reference on relevant script once then you can play with it.

If I edit your code, then it should be something like this

public class  Controller : MonoBehaviour {

    Speedy car;

    Start()
    {
        car = FindObjectOfType<Speedy>();
    }

     if (speed == good)
        {

            if (Input.GetMouseButtonUp (0)) 
            {
                car.enabled = false;
            }

            else car.enabled = true;
        }

}

Drop this script to another GameObject in hierarchy.

NOTE: In your question you are disabling script from car but enabling if from cube. I don't know that you did it intentionally.

\$\endgroup\$

You must log in to answer this question.