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 lonedrow1 · Sep 04, 2014 at 10:34 PM · raycastnullreferenceexceptiongetcomponent

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;
 }
 }


Comment
Add comment · Show 3
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 lonedrow1 · Sep 04, 2014 at 10:47 PM 0
Share

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.

avatar image rutter · Sep 04, 2014 at 11:52 PM 0
Share

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

avatar image lonedrow1 · Sep 05, 2014 at 12:03 AM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

RaycastHit2D.collider is null - why? 1 Answer

InitializeOnLoad = System.NullReferenceException 1 Answer

Calling GetComponent on an instantiated prefab returns null,Calling GetComponent on an instantiated prefab 1 Answer

GetComponent Picking up multiple scripts from differrent objects? 1 Answer

C# - Auto increment of Scenes through Collision 0 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