Billboard script is not working in unity 2020

When I was testing my unity game It starting showing errors and I realized the sprite could not rotate.

Error:
NullReferenceException: Object reference not set to an instance of an object
Billboard.Update ()

Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Billboard : MonoBehaviour
{
void Update()
{
transform.rotation = Quaternion.Euler(0f, Camera.main.transform.rotation.eulerAngles.y, 0f);
}
}

How do I fix this.

NullReferenceException means you’ve tried to dig deeper into an object that does not exist. In other words, that’s almost always, “something to the left of a dot on the indicated line is null”.

On this post we have a Simpsons Halloween Episode sequence of thoughts:

You didn’t use code tags, and the error code given wasn’t copied directly out of the console so it doesn’t include the line number. That’s bad
But, there’s only effectively one line of code in this script that could possibly cause a NRE. That’s good!
But, the line in question has six “dots” we have to check to see if the left-side of that dot could be null. That’s bad (usually you would want to break up complex lines of code into multiple lines for easier debugging)

But, the left sides of most of those dots are either class names (Quaternion, Camera) or structs (rotation, eulerAngles), neither of which can ever be null. That’s good!
But the frogurt is also cursed. Can I go now?

By process of elimination, the only one of these that can ever really be null is Camera.main, which is a convenience property that finds a Camera object which is tagged with the MainCamera tag. So, you must not have a camera with that tag in your scene.

2 Likes