0
\$\begingroup\$

I have the CarController script inside my car GameObject. I need to set it to disabled at the start so the countdown timer can start the race.

I thought I knew how to do it but once again I have failed :/

Any ideas why this doesn't work??

void TogglePlayerEnabled(bool b)
{
    playerCar.GetComponent<CarController>().enabled = b;
}
void ToggleAiCarEnabled(bool b)
{
    aiCar.GetComponent<CarController>().enabled = b;
}

If I use a print line saying "Toggled" or something, that appears in console as expected. But the CarController script seems like it stays active as both me, and the AI car can just drive whilst the countdown is happening still.

Here's the full class:

public class GameManagerControl : MonoBehaviour {

public GameObject playerCar;
public GameObject aiCar;

public int sceneChangeZValue;

private string countdown = "";    
private bool showCountdown = true;




// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
    // TODO: This will change the scene when the track ends.
    //if (player.transform.position.z >= sceneChangeZValue)
    //      {

    //          SceneManager.LoadScene("race_track_lake");
    //      }

    if (showCountdown)
    {
        StartCoroutine(GetReady());
    }


}

// call this function to display countdown
IEnumerator GetReady()
{
    countdown = "3";
    TogglePlayerEnabled(false);
    ToggleAiCarEnabled(false);
    yield return new WaitForSeconds(1.5f);


    countdown = "2";

    yield return new WaitForSeconds(1.5f);


    countdown = "1";

    yield return new WaitForSeconds(1.5f);


    countdown = "GO";
    TogglePlayerEnabled(true);
    ToggleAiCarEnabled(true);
    yield return new WaitForSeconds(1.5f);


    showCountdown = false;
    countdown = "";
}

// GUI
void OnGUI()
{
    if (showCountdown)
    {
        GUI.color = Color.red;
        GUI.Box(new Rect(Screen.width / 2 - 100, 50, 200, 175), "GET READY");

        // display countdown    
        GUI.color = Color.white;
        GUI.Box(new Rect(Screen.width / 2 - 90, 75, 180, 140), countdown);
    }
}
\$\endgroup\$
0
\$\begingroup\$

Damn Ive done it again and fixed it just after righting up this question!! Something about typing it up here helps me fix it i dunno why lol.

Basically, I had another script attached to each can for UserControl and AiControl, and I just had to add those in with the other enable.disable calls in the Toggle methods.

Sorry to waste anyones time.... again!

\$\endgroup\$
  • \$\begingroup\$ Nice ^^ no worry though ;) \$\endgroup\$ – derHugo Jul 20 '17 at 17:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.