Search Unity

Hat Tricks 2D game Game Controller Script Error

Discussion in 'Getting Started' started by vidit0210, Feb 2, 2016.

  1. vidit0210

    vidit0210

    Joined:
    Dec 30, 2015
    Posts:
    30
    Error in renderer statement.
    statement : float ballWidth = balls[0].renderer.bounds.extents.x;
    full code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HT_GameController : MonoBehaviour {
    5.    
    6.     public Camera cam;
    7.     public GameObject[] balls;
    8.     public float timeLeft;
    9.     public GUIText timerText;
    10.     public GameObject gameOverText;
    11.     public GameObject restartButton;
    12.     public GameObject splashScreen;
    13.     public GameObject startButton;
    14.     public HT_HatController hatController;
    15.    
    16.     private float maxWidth;
    17.     private bool counting;
    18.    
    19.     // Use this for initialization
    20.     void Start () {
    21.         if (cam == null) {
    22.             cam = Camera.main;
    23.         }
    24.         Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
    25.         Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner);
    26.         float ballWidth = balls[0].renderer.bounds.extents.x;
    27.         maxWidth = targetWidth.x - ballWidth;
    28.         timerText.text = "TIME LEFT:\n" + Mathf.RoundToInt (timeLeft);
    29.     }
    30.  
    31.     void FixedUpdate () {
    32.         if (counting) {
    33.             timeLeft -= Time.deltaTime;
    34.             if (timeLeft < 0) {
    35.                 timeLeft = 0;
    36.             }
    37.             timerText.text = "TIME LEFT:\n" + Mathf.RoundToInt (timeLeft);
    38.         }
    39.     }
    40.  
    41.     public void StartGame () {
    42.         splashScreen.SetActive (false);
    43.         startButton.SetActive (false);
    44.         hatController.ToggleControl (true);
    45.         StartCoroutine (Spawn ());
    46.     }
    47.  
    48.     public IEnumerator Spawn () {
    49.         yield return new WaitForSeconds (2.0f);
    50.         counting = true;
    51.         while (timeLeft > 0) {
    52.             GameObject ball = balls [Random.Range (0, balls.Length)];
    53.             Vector3 spawnPosition = new Vector3 (
    54.                 transform.position.x + Random.Range (-maxWidth, maxWidth),
    55.                 transform.position.y,
    56.                 0.0f
    57.             );
    58.             Quaternion spawnRotation = Quaternion.identity;
    59.             Instantiate (ball, spawnPosition, spawnRotation);
    60.             yield return new WaitForSeconds (Random.Range (1.0f, 2.0f));
    61.         }
    62.         yield return new WaitForSeconds (2.0f);
    63.         gameOverText.SetActive (true);
    64.         yield return new WaitForSeconds (2.0f);
    65.         restartButton.SetActive (true);
    66.     }
    67. }
    68.  
    Moreover no public variables are seen in Inpector.Only this is seen
    http://postimg.org/image/yltux0063/


    same Goes for StartGame function
    error:
     
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    The error message is telling you what is wrong & what to do to fix it for the renderer error.