Search Unity

Hello everyone I have got some trouble with script of BallController

Discussion in 'Scripting' started by Deleted User, Apr 21, 2019.

  1. Deleted User

    Deleted User

    Guest

    Error is "NullReferenceException: object reference not set to an instance of an object"
    Unity tells me , trouble in the string
    Code (CSharp):
    1. Camera.main.GetComponent<CameraFollow>().gameOver = true;
    please help me to fix that.
    Full code :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BallController : MonoBehaviour {
    6.     [SerializeField]
    7.     private float speed;
    8.     bool started;
    9.     Rigidbody rb;
    10.     bool gameOver;
    11.  
    12.     void Awake()
    13.     {
    14.         rb = GetComponent<Rigidbody>();
    15.      
    16.     }
    17.     // Use this for initialization
    18.     void Start () {
    19.         started = false;
    20.         gameOver = false;
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update () {
    25.  
    26.         if(!started)
    27.         {
    28.             if(Input.GetMouseButtonDown (0))
    29.             {
    30.                 rb.velocity = new Vector3(speed, 0, 0);
    31.                 started = true;
    32.             }
    33.         }
    34.  
    35.         Debug.DrawRay(transform.position, Vector3.down, Color.red);
    36.  
    37.        if(!Physics.Raycast (transform.position, Vector3.down, 1f))
    38.         {
    39.             gameOver = true;
    40.             rb.velocity = new Vector3(0, -25f, 0);
    41.          
    42.                 Camera.main.GetComponent<CameraFollow>().gameOver = true;
    43.          
    44.         }
    45.  
    46.         if(Input.GetMouseButtonDown (0) && !gameOver)
    47.         {
    48.             SwitchDirection ();
    49.         }
    50.     }
    51.  
    52.     void SwitchDirection ()
    53.     {
    54.         if (rb.velocity.z > 0)
    55.         {
    56.             rb.velocity = new Vector3(speed, 0, 0);
    57.         }
    58.         else if (rb.velocity.x > 0)
    59.         {
    60.             rb.velocity = new Vector3(0, 0, speed);
    61.         }
    62.     }
    63. }
    64.  
    Full Script for camera follow
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraFollow : MonoBehaviour {
    6.  
    7.     public GameObject ball;
    8.     Vector3 offset;
    9.     public float lerpRate;
    10.     public bool gameOver;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         offset = ball.transform.position - transform.position;
    15.         gameOver = false;
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update () {
    20.         if (!gameOver)
    21.         {
    22.             follow();
    23.         }
    24.     }
    25.  
    26.     void follow()
    27.     {
    28.         Vector3 pos = transform.position;
    29.         Vector3 targetPos = ball.transform.position - offset;
    30.         pos = Vector3.Lerp(pos, targetPos, lerpRate * Time.deltaTime);
    31.         transform.position = pos;
    32.     }
    33. }
    34.  
    I will be very grateful.
     
  2. MiRRRa

    MiRRRa

    Joined:
    Feb 18, 2016
    Posts:
    8
    Show please screenshot of your game in editor , especially inspector with Main Camera selecting, because i think there is no CameraFollow script on your camera