- Home /
GetComponent, Object Reference not set to an instance of an object
Hey all, love this site on so many levels. Time to ask a question of my own, though.
I have a script I am using to attach to doors (OpenableDoor), and passing it an "OpenDoor" script attached to my camera so it can read a raycast in order to determine if the player is actually looking at the door.
Now, this works perfectly fine. When the PlayTest first runs, it throws a NullReferenceException the first frame for all of the doors this is attached to, I am assuming because the raycast hasn't had a chance to properly hit something yet. All of the doors open perfectly fine, so on and so forth, and a NullReferenceException is not thrown until I end the PlayTest.
The issue I am having is with my "OpenLargeGarage" script. It is a near exact copy of my "OpenableDoor" script, except it uses a transform instead of a rotation (obviously, for a different kind of door). Unlike my other doors, however, it is constantly throwing out multiple NullReferenceExceptions on every frame, even though it is referencing the same camera the "OpenableDoor" script is. The error is thrown on line 8 of the "OpenLargeGarage" script.
EDIT: I also forgot to mention that it will not let me open the door that has the "OpenLargeGarage" script on it.
If anyone can help me, it would be appreciated. I have been researching for the past few days trying to fix this, but it is completely baffling how one script is working with the main camera, while the second script says the script on the main camera is Null. I really have no idea what is going on.
It wont let me attach my scripts, saying the file type is invalid. Not sure what's up with that.
As a quick re-cap: "OpenableDoor" is attached to a door. I feed it both a door object, and the main camera. "OpenLargeGarage" is attached to a different kind of door. I feed it both a door object, and the main camera. "OpenableDoor" does not through NullReferenceExceptions, while "OpenLargeGarage" does.
Here is the "OpenableDoor" script:
// Smothly open a door
var smooth = 2.0;
var DoorOpenAngle = 115.0;
private var open : boolean;
private var enter : boolean;
var door : GameObject;
public var targetObj : Transform;
public var targetScript : OpenDoor;
private var defaultRot : Vector3;
private var openRot : Vector3;
function Start(){
targetScript = targetObj.GetComponent(OpenDoor);
defaultRot = transform.eulerAngles;
openRot = new Vector3 (defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z);
}
//Main function
function Update (){
if(open){
//Open door
door.transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
}else{
//Close door
door.transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
}
if(Input.GetKeyDown("e") && enter == true && targetScript.raycheck == true){
open = !open;
}
}
function OnGUI(){
if(enter){
//GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 30), "Press 'F' to open the door");
}
}
//Activate the Main function when player is near the door
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
enter = true;
}
}
//Deactivate the Main function when player is go away from door
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") {
enter = false;
}
}
This is the "OpenDoor" script:
#pragma strict
private var guiShow : boolean = false;
private var isOpen : boolean = false;
//var door : GameObject;
public var raycheck = false;
var rayLength = 10;
function Update()
{
var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
raycheck = false;
if(Physics.Raycast(transform.position, fwd, hit, rayLength))
{
if(hit.collider.gameObject.tag == "Door")
{
raycheck = true;
guiShow = true;
if(Input.GetKeyDown("e") && isOpen == false)
{
//door.animation.CrossFade("DoorOpen");
isOpen = true;
guiShow = false;
}
else if(Input.GetKeyDown("e") && isOpen == true)
{
//door.animation.CrossFade("DoorClose");
isOpen = false;
guiShow = false;
}
}
}
else
{
guiShow = false;
}
}
function OnGUI()
{
if(guiShow == true && isOpen == false && raycheck == true)
{
GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 100, 25), "Open Door");
}
if(guiShow == true && isOpen == true && raycheck == true)
{
GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 100, 25), "Close Door");
}
}
Here is the "OpenLargeGarage" script:
// Smothly open a door
var smooth = 2.0;
var DoorOpenHeight = 3;
private var open : boolean;
private var enter : boolean;
var door : GameObject;
public var targetObj : Transform;
public var targetScript : OpenDoor ;
private var defaultPos : Vector3;
private var openPos : Vector3;
function Start(){
targetScript = targetObj.GetComponent(OpenDoor);
defaultPos = transform.position;
openPos = new Vector3 (defaultPos.x, defaultPos.y + DoorOpenHeight, defaultPos.z);
}
//Main function
function Update (){
if(open){
//Open door
door.transform.position = Vector3.Lerp(transform.position, openPos, Time.deltaTime * smooth);
}else{
//Close door
door.transform.position = Vector3.Lerp(transform.Position, defaultPos, Time.deltaTime * smooth);
}
if(Input.GetKeyDown("e") && enter == true && targetScript.raycheck == true){
open = !open;
}
}
function OnGUI(){
if(enter){
//GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 30), "Press 'F' to open the door");
}
}
//Activate the Main function when player is near the door
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
enter = true;
}
}
//Deactivate the Main function when player is go away from door
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") {
enter = false;
}
}
Interesting. That seems to have gone some way to solving the issue. The regular doors no longer throw a NullReferenceException (the doors attatched with the "openableDoor" script), but the door attached with the "OpenableLargeGarage" script is still throwing NullReferenceExceptions like crazy. I have edited the code in my question to reflect the change.
This line in your Start function:
var targetScript : OpenDoor = targetObj.GetComponent(OpenDoor);
Should be:
targetScript = targetObj.GetComponent(OpenDoor);
The current version declares a new variable "targetScript" that only exists within the Start function; although it has the same name as the original, the compiler sees two distinct variables. By avoiding the "var" declaration, you'll just assign a value to the original.
Aren't computers great? :D
Yes, computers are wonderful.
The new change is now not having a variable just exist in the start function, but the issue with the "OpenLargeGarage" script is still persisting. Null Exceptions. Null Exceptions, everywhere.