Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by kubastick · Aug 05, 2016 at 05:09 PM · errortransformmultiplayercanvas

(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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image btmedia14 · Aug 05, 2016 at 06:17 PM 0
Share

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.

avatar image TBruce btmedia14 · Aug 06, 2016 at 05:03 PM 0
Share

@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.

avatar image btmedia14 TBruce · Aug 06, 2016 at 06:25 PM 0
Share

@$$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.

avatar image TBruce · Aug 07, 2016 at 08:29 PM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

112 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cant Shoot client, Unet 0 Answers

[UNET] Network Transform doesnt work for the host. On Client it works seemless. 0 Answers

Transform Position Are Different Each Other 1 Answer

Multiplayer: One Shared Interactive Canvas for both players 0 Answers

Multiplayer - Host and Client problem with input. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges