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 Xentarok · Jul 31, 2013 at 05:52 PM · c#cameranullreferenceexception

[Solved]NullReferenceException problem

Hello.

I have a script attached to planets that makes camera go from "Solar view" to "Planet view" in a smooth lerp. Here are pictures of both views:

alt text

alt text

Here's the script:

 using UnityEngine;
 using System.Collections;
 
 public class CamSol : MonoBehaviour {
 
     public Vector3 DefPos = new Vector3(0,600,0);
     public Camera Cam;
     public Transform Target;
     public float MinDis = 50f;
     
     private float MoveChange = 0f;
     private float t;
     public float RandomFloat;
     
     
     void Start () {
         
         }
     
     
     void OnMouseUp(){
     
         MoveChange++;
     
     }
     
     
     void Update () {
         
         Camera Cam = Camera.current;
 
         
         
         
         transform.RotateAround(new Vector3(0,0,0), Vector3.up, RandomFloat *  Time.deltaTime);
         Transform Target = transform.transform; 
         
         
         
         if(MoveChange == 1f  ){
 
             
              
         
             t+=Time.deltaTime;
             
             Vector3 Smth = Target.transform.position - Cam.transform.position;
             Quaternion Rot = Quaternion.LookRotation(Smth);
             Cam.transform.rotation = Quaternion.Lerp(Cam.transform.rotation, Rot, t/5);
             
             Vector3 Pos = new Vector3(Target.transform.position.x, Target.transform.position.y, Target.transform.position.z -MinDis);
             
             Cam.transform.position = Vector3.Lerp(DefPos, Pos, t * 1.5f);  
             
             
             
         }
         
         if(MoveChange == 2f){
             
             t = 0f;
             t+=Time.deltaTime;
             
             Quaternion Rot = Quaternion.Euler(90,0,0);        
             
             Cam.transform.position = Vector3.Lerp(Cam.transform.position, DefPos, t*1.5f);    
             Cam.transform.rotation = Quaternion.Lerp (Cam.transform.rotation, Rot, t*1.5f);
             
         }
     
     if(MoveChange == 3f){
             
             
             MoveChange = 1f;
             
     }    
     
     }
 
 
 }



[Edit] Problem solved, you need to change Camera.current into Camera.main

The probelm is, that if I try to go to Planet view in editor I get NullReferenceException Error and the camera is spazzing in the game.

Thanks in advance for any answears.

-Xentarok

P.S: Sorry for any mistakes.

P.S.2: If you have any questions or I didn't explain something in enough detail, write it in the comments.

plaview.jpg (131.9 kB)
solview.jpg (80.9 kB)
Comment
Add comment · Show 6
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 perchik · Jul 31, 2013 at 05:56 PM 0
Share

What does the actual error say (line number?)

avatar image Dave-Carlile · Jul 31, 2013 at 06:02 PM 3
Share

On a side note, at first glance your "exit" button looks like it says "shit".

avatar image Xentarok · Jul 31, 2013 at 06:38 PM 0
Share

Full error message: NullReferenceException UnityEngine.Component.get_transform () (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineComponent.cs:21) CamSol.Update () (at Assets/Game/$$anonymous$$ody/CamSol.cs:69)

avatar image perchik · Jul 31, 2013 at 06:48 PM 0
Share

In your script, what does line 69 correspond with in the code you posted?

avatar image perchik · Jul 31, 2013 at 06:51 PM 0
Share

Well, since the error is with get_transform, it suggests that you are trying to use the transform of some object that doesn't exist. This leads me to the conclusion that in line 66 above,

Cam.transform.position = Vector3.Lerp(Cam.transform.position, DefPos, t*1.5f);

is the error, since the compiler thinks Cam doesn't exist.

Based on the documentation, I'm not entirely sure what Camera.current should actually return. What do you expect it to give you?

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by tw1st3d · Jul 31, 2013 at 06:12 PM

 void Update () {
  
     Camera Cam = Camera.current;

should be

 void Update () {
  
     Cam = Camera.current;

I believe, cause it looks like you're definine "Cam" twice.

Comment
Add comment · Show 1 · 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 Xentarok · Jul 31, 2013 at 06:36 PM 0
Share

Sorry, but it isn't working that way either.

avatar image
0

Answer by aibolith · Apr 16, 2017 at 04:57 PM

I had a similar problem: I was taking Camera.current.transform.rotation to adjust characters UI labels, and it was working, but it was giving me the null refference exception every single frame. I changed it to Camera.main and the exception vanished.

What I wonder is how is possible for the code to work even though it says that I'm trying to refference a null object? Where did it take correct .transform.rotation , if the object was null?

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

19 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

Related Questions

NullRefenceException error 1 Answer

Im Getting an error saying: Object reference not set to an instance of an object although nothing is null. 1 Answer

NullReferenceException .. problem with Camera.main.transform 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers


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