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
@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);
}
}