(C#)Object reference not set to an instance of an object
My ultra simple script:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class Bilboard : NetworkBehaviour {
void FixedUpdate() {
transform.LookAt (Camera.current.transform);
}
}
If i using this script in game i get error:Object reference not set to an instance of an object in debug ,but script is working,is attached to canvas. Sry for bad English
Answer by TBruce · Aug 05, 2016 at 06:03 PM
@kubastick The problem is most likely that Camera.current is not initialized (this commonly happens). There are several ways to handle this (basically by creating your own reference to the camera).
You can do this
public Camera camera; // you will need to set this in the inspector
void FixedUpdate()
{
if (camera != null)
{
transform.LookAt(camera.transform);
}
}
or this
private Camera camera;
void Start ()
{
camera = Camera.main;
}
void FixedUpdate()
{
if (camera != null)
{
transform.LookAt(camera.transform);
}
}
or this
private Camera camera;
void Start ()
{
if ((GameObject.Find("Main Camera") != null) && (GameObject.Find("Main Camera").GetComponent<Camera>() != null))
{
camera = GameObject.Find("Main Camera").GetComponent<Camera>();
}
}
void FixedUpdate()
{
if (camera != null)
{
transform.LookAt(camera.transform);
}
}
or this
private Camera camera;
void Start ()
{
if (gameObject.GetComponent<Camera>() != null))
{
camera = gameObject.GetComponent<Camera>();
}
}
void FixedUpdate()
{
if (camera != null)
{
transform.LookAt(camera.transform);
}
}
or you can do a combination
public Camera camera; // you will need to set this in the inspector
void Start ()
{
if (camera == null)
{
Debug.Log("Billboard: Camera has not been initialized in the inspector. Please set in the inspector and try again. Getting the current camera.");
if (gameObject.GetComponent<Camera>() != null))
{
camera = gameObject.GetComponent<Camera>();
}
else
{
camera = Camera.main;
else if ((camera == null) && (GameObject.Find("Main Camera") != null) && (GameObject.Find("Main Camera").GetComponent<Camera>() != null))
{
camera = GameObject.Find("Main Camera").GetComponent<Camera>();
}
}
}
}
void FixedUpdate()
{
if (camera != null)
{
transform.LookAt(camera.transform);
}
}
but the first method is the most easiest.
Hey @$$anonymous$$avina as always amazing replies(yeah a fan here). Just wondering though, typically wouldn't the LookAt method be used the other way around. That is, the script would be attached to the camera and the object to be "looked at" would be the the transform indicated here? Just a thought.
@btmedia14 Usually this is the case. I do not know what the intentions of @kubastick are.
But the way this script is written the GameObject that this class is attached to will constantly rotate to look at a given camera.
Look at it like this, the camera is attached to the player. You have a GameObject (in this case a BillBoard) with the current class attached to it. As the player walks by/around the BillBoard gameObject, the BillBoard will rotate to look at the player.
Note: As previously stated, I do not know what the intentions of @kubastick are. I only gave the answer for removing the error.
@$$anonymous$$avina A rotating billboard - of course, forehead slap! That's an interesting example and I had not thought of that perspective of an object always staring back at you (or the player) as you move around. I guess like the eyes in a portrait that follow you no matter which direction you look from. Agreed, cannot assume the intent of the op. $$anonymous$$any thanks.
@kubastick Would you be so kind as to click the "Accept" button to accept the answer if your question was answered to your satisfaction. Thank you!
Your answer
