Search Unity

Calling other variables problem.

Discussion in 'Scripting' started by nerdboystudios, Jul 25, 2011.

  1. nerdboystudios

    nerdboystudios

    Joined:
    Jan 9, 2011
    Posts:
    56
    I'm currently working on a racing game where the player has a limited amount of time to reach the finish line before the car blows-up. I have a script that controles the timer and I'm trying to get the car to not take the input of the player after time expires. I'm having a problem with this so please help. Thanks in advance.

    The Script
    Code (csharp):
    1.  
    2. var skin:GUISkin;
    3. var player:Transform; var on : boolean = true;
    4. private var startTime;
    5. private var restSeconds : int;
    6. private var roundedRestSeconds : int;
    7. private var displaySeconds : int;
    8. private var displayMinutes : int;
    9. var explosionPrefab : GameObject;
    10. var explosionLife : float = 2;
    11. var countDownSeconds : int;
    12. var heartbeat : AudioClip;
    13. function Awake() {
    14.     startTime = Time.time;
    15. }
    16. function OnGUI () {
    17.         if (skin != null) {
    18.         GUI.skin = skin;
    19.     }
    20.     var guiTime = Time.time - startTime;
    21.     restSeconds = countDownSeconds - (guiTime);
    22.     if (restSeconds == 60) {
    23.         print ("One Minute Left");  
    24.     }
    25.     if (restSeconds == 12) {
    26.         audio.clip = heartbeat;
    27.         audio.Play(44100);
    28.     }
    29.     if (restSeconds == 0) {
    30.         print ("Time is Over");
    31.         var expPos = transform.position;
    32.         var exp: GameObject = Instantiate (explosionPrefab, expPos, Quaternion.identity);
    33.         Destroy(exp, explosionLife);
    34.         var other : CarControl = gameObject.GetComponent(CarControl);
    35.         other.motorInput = 0;
    36.         other.brakeInput = 1;
    37.         //player.GetComponent(CarControl).enabled = false;
    38.         //player.GetComponent(SoundController).enabled = false;
    39.         player.GetComponent(CarSettings).enabled = false;
    40.         player.GetComponent(CarVisuals).enabled = false;
    41.         Time.timeScale = 1;
    42.     }
    43.     roundedRestSeconds = Mathf.CeilToInt(restSeconds);
    44.     displaySeconds = roundedRestSeconds % 60;
    45.     displayMinutes = roundedRestSeconds / 60;
    46.     text = String.Format (" {0:00}:{1:00}", displayMinutes, displaySeconds);
    47.     GUI.Box (new Rect (Screen.width-200, 0, 180, 40), text);
    48.     }
    49.  
     
  2. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    you can disable the input script on the car by using GetComponent()
     
  3. nerdboystudios

    nerdboystudios

    Joined:
    Jan 9, 2011
    Posts:
    56