Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by RLin · May 10, 2015 at 08:36 PM · c#nullreferenceexceptionconsolespam

Unity spammed with NullReferenceExceptions

I have a script to always parent an object to the current camera and set its local position to that of the camera. The script works fine, however the console is spammed with NullReferenceExceptions occurring on line 7 of the script. This makes it very annoying and difficult to find true errors.

Here is the script:

 using UnityEngine;
 using System.Collections;
 
 public class MoveToCam : MonoBehaviour {
     void Update () {
         transform.localPosition = new Vector3 (0, 0, 0);
         transform.parent = Camera.current.transform;
     }
 }
 

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

2 Replies

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

Answer by Rtyper · May 10, 2015 at 08:45 PM

Setting transform.parent is something you only need to do once. After that, it's a child of the other object so it will stay in the same place relative to the parent object. You'd be better off doing this in Start rather than Update. That's why it keeps coming up.

The null reference exception itself means that Camera.current is null. Make sure your camera is tagged as MainCamera, that's what determines Camera.current.

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 RLin · May 10, 2015 at 09:24 PM 0
Share

The problem is, the current camera is constantly changing. Pressing shift switches the camera to another one, so I have to keep it in update for the parenting to function correctly.

avatar image Cherno · May 10, 2015 at 09:27 PM 0
Share

Why don't you keep a reference to the script and each time you switch cameras, the new one is assigned to a variable in the script.

avatar image RLin · May 10, 2015 at 09:29 PM 0
Share

I guess that will work, i will try it.

avatar image RLin · May 10, 2015 at 09:33 PM 0
Share

Works fine now.

avatar image
1

Answer by MANDAL0R3 · May 10, 2015 at 10:19 PM

Camera.Current is returning null at some points of the call. Normally when i run across this error it means that something that is trying to be accessed is null. To fix the problem just add this new line of code and it should all work fine.

 using UnityEngine;
 using System.Collections;
 
 public class MoveToCam : MonoBehaviour{
     void Update(){
         transform.localPosition = new Vector3(0, 0, 0);
         if (Camera.current != null)
         {
             transform.parent = Camera.current.transform;
         }
     }
 }
 
Comment
Add comment · 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

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Got a Error about reffering a object i can not fix 0 Answers

NullReferenceException and m_InstanceID == 0 on LoadLevel (C#) 1 Answer

NullReferenceException in my cheat code? 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