- Home /
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
maybe you dont have a camera tagged "mainCamera". You could try camera.current .
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.
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.
@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.
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.
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613083217im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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