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
-1
Question by owags7 · Nov 01, 2012 at 08:51 PM · camerajavascripttransform

NullReferenceException Error

I am trying to write a script that will move a group from its current position to an "Examine" position. This will happen upon clicking a button. Also, a cube will appear, and the camera will move to a designated spot. Here's my current script:

 #pragma strict
 private var outside1:Transform;
 private var originalspotTC: Transform;
 private var moved = false;
 static var main: Camera;
 private var CameraBlack: Transform;
 private var CameraStart: Transform;
 private var BlackPicture: GameObject;
 private var stringToEdit: String ="Some stuff";
 function Start(){
     originalspotTC=transform.Find("/OriginalAdenine");
     outside1=transform.Find("/ExamineAdenine");
     CameraBlack=transform.Find("/CameraA");
     CameraStart=transform.Find("/MainCameraStart");
     BlackPicture=GameObject.Find("/CubeA");
 }
 function OnGUI(){
     GUILayout.BeginHorizontal();
     GUILayout.Space(200);
     
     if(GUI.Button(Rect(50,50,150,50),"Examine A")){
         if(moved){
             moved=false;
             BlackPicture.renderer.enabled=false;
         }
         else
         {
             moved=true;
         }
     }
     GUILayout.EndHorizontal();
     
     if(moved){
         transform.position=outside1.position;
         stringToEdit=GUI.TextArea(Rect(800,100,100,100),stringToEdit,200);
         Camera.main.transform.position=CameraBlack.position;
         BlackPicture.renderer.enabled=true;
     }
 else{
         transform.position=originalspotTC.position;
         Camera.main.transform.position=CameraStart.position;
         BlackPicture.renderer.enabled=false;
 }
 }

Any help on the error: NullReferenceException Adenine.OnGUI () (at Assets/Adenine.js:41) and NullReferenceException Adenine.OnGUI () (at Assets/Adenine.js:36)

Thanks

Comment
Add comment · Show 5
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 Polymo · Nov 01, 2012 at 08:57 PM 0
Share

maybe you dont have a camera tagged "mainCamera". You could try camera.current .

avatar image DoctorWhy · Nov 01, 2012 at 09:11 PM 0
Share

Which line is it that is throwing the error? Considering we don't have line numbers, it would be a hassle for us to count.

If I did count right, though, then either BlackPicture or BlackPicture.renderer is null.

avatar image DoctorWhy · Nov 01, 2012 at 09:17 PM 0
Share

And I don't mean to sound rude when I say this, but I will be blunt. Learning to debug is an essential skill to learn as a programmer. If you are going to be writing code, please put a little more effort into trying to narrow down the problem, then come ask for help if you don't know. Unity offers great debugging tools, including double clicking on the error and it then taking you to the exact line where the error occurred.

A Null reference exception is on of the easiest errors to understand. You either didn't assign something correctly (resulting in a null/empty variable when you try to access it), or something wasn't found, which results in the null/empty variable.

avatar image owags7 · Nov 01, 2012 at 11:35 PM 0
Share

@DoctorWhy I was in a hurry to post this and was not clear enough about my problem. Firstly, I know what a Null reference exception is and I know how to debug. Secondly, I have spent quite a bit of time looking at: Camera.main.transform.position=CameraBlack.position; and Camera.main.transform.position=CameraStart.position; I wouldn't immediately come here for "one of the easiest" errors. I am wondering if the error can be anything else, because I have checked both of the solutions you suggested multiple times.

avatar image DoctorWhy · Nov 01, 2012 at 11:40 PM 0
Share

It is good you know how to debug. Even if you were in a hurry, all you did was post what you wanted to do, your whole class, then the error, not explaining what you have tried, what you may not understand, or even the exact line that is throwing the error. It would quicken getting a solution if you could explain more.

Again, I didn't mean to be rude, but have a wall of code, and simply telling us your error seems like you didn't put any work into the finding a solution yourself.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by DaveA · Nov 01, 2012 at 09:54 PM

This must have failed:

CameraStart=transform.Find("/MainCameraStart");

Make sure you have a Camera on an object called MainCameraStart and that it is at the top level of the hierarchy

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

13 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

Related Questions

LookAround Up&Down! 1 Answer

Move an object toward an angle in 2d space 0 Answers

Return camera to original rotation values Logic 1 Answer

Pan Orthographic Camera 0 Answers

Camera, Offsetting the Transform.LookAt 2 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