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\$
  • \$\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\$ – Almo Jan 12 '16 at 20:36
  • \$\begingroup\$ @Almo Title edited. \$\endgroup\$ – user77303 Jan 12 '16 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\$ – Hamza Hasan Jan 12 '16 at 20:39
  • \$\begingroup\$ @HamzaHasan That's my question. Is there another way? \$\endgroup\$ – user77303 Jan 12 '16 at 20:40
  • \$\begingroup\$ Control all of these operations from another class. \$\endgroup\$ – Hamza Hasan Jan 12 '16 at 20:43
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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy