Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Camera-Based Reflections no longer working

Discussion in 'Scripting' started by ATLAS-INTERACTIVE, Jan 20, 2016.

  1. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I put this script I have been using a lot in other projects in a new project, but I am now getting an error when I try to run it.

    The error:
    Object reference not set to an instance of an object
    RealtimeReflection.Update () (at Assets/RealtimeReflection.cs:15)

    The code:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class RealtimeReflection : MonoBehaviour
    4. {
    5.  
    6.     ReflectionProbe probe;
    7.  
    8.     void Awake()
    9.     {
    10.         probe = GetComponent<ReflectionProbe>();
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         probe.transform.position = new Vector3(
    16.             Camera.main.transform.position.x,
    17.             Camera.main.transform.position.y * -1,
    18.             Camera.main.transform.position.z
    19.         );
    20.  
    21.         probe.RenderProbe();
    22.     }
    23. }
     
  2. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Does the gameobject have a ReflectionProbe attached? You should add a RequiresComponent attribute if the script NEEDS a certain component to actually function.
     
  3. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    The script is sitting on a ReflectionProbe object which is a child of my camera.
     
  4. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Do you have a camera in your scene which is tagged as "MainCamera" ?

    It's either the Camera or the probe which is null.
     
  5. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I think I found the problem, the probe moves far below what it should do.
    The probe moves to around -10Y, but also has a random position for X and Z, which I don't understand.

    This never happened before the new project, and I have changed nothing, but it isn't working as it should, or did.