Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Jafar009 · May 28, 2014 at 11:36 PM · javascripterrorgetcomponentinstanceobject reference

"Object reference not set to an instance of an object" Error

Hi all, I am attempting to create a function, located in the main Camera object, that is called by the player object when something specific happens. The function simply inverts the value of a float in the Camera object by multiplying it against -1.

However, whenever the event happens in the game (the player passing a specific trigger) I am met with the error "Object reference not set to an instance of an object". I'm using GetComponent to attempt to run the function that is in the camera's script, but this does not seem to work (in the BeeReverse function below). The next few lines are another GetComponent that is accessing a variable from a different script on the player using the same syntax and it is working fine.

Does anyone have any idea why my attempts to run the function from the camera object are failing?

This is the player's Code:

 var player : GameObject;
 player = GameObject.FindWithTag("Player");
 var beeCam : Camera;
 beeCam = Camera.main;
 // These variables are used for the player's health and health bar
 var curHP : float=3;
 var maxHP : float=3;
 var gems : int = 0;
  
 function Update(){
     
     if (curHP > maxHP){
         curHP = maxHP;
     }
     
     if (Input.GetKeyDown("escape")){
         Application.LoadLevel(0);
     }
 }
     
 function ChangeHP(Change:float) {
     // This line will take whatever value is passed to this function and add it to curHP.
     curHP += Change;
      
     // This if statement ensures that we don't go over the max health
     if(curHP>maxHP) {
         curHP=maxHP;
     }
      
     // This if statement is to check if the player has died
     if(curHP<=0) {
         Application.LoadLevel(Application.loadedLevel);  //restarts level
         Debug.Log("Player has died!");
     }
 }
 
 function BeeReverse(){
     var camScript : LookAtBee = beeCam.GetComponent(LookAtBee);
     camScript.Flip();   //turns little camera around!
     var moveScript : CharacterControlling = player.GetComponent(CharacterControlling);
     moveScript.fallSpeed = (moveScript.fallSpeed * -1);  
     transform.Rotate(0,180,0);   //turns little bee around!
     
 }


This is the camera's code I'm attempting to access:

 var target : Transform;
 public var offset : float = 326.05;
 
 @script AddComponentMenu("Camera-Control/Smooth Look At")
 
 function LateUpdate () {
     transform.position.y = (target.position.y) - offset;
 }
 function Start () {
     // Make the rigid body not change rotation
        if (rigidbody)
         rigidbody.freezeRotation = true;
 }
 
 function Flip(){
     offset = (offset * -1);
 }
Comment
Add comment · Show 7
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 meat5000 ♦ · May 28, 2014 at 11:37 PM 0
Share

If it helps, I tend to find I get NullRefs etc when trying to access cameras sometimes, even though the Camera is found and the function works...it still throws the error. BIZARRRROOOOO

avatar image rutter · May 28, 2014 at 11:39 PM 0
Share

The error message should indicate a specific file and line number. If you know which line is a problem, you'll know which reference isn't set.

avatar image meat5000 ♦ · May 28, 2014 at 11:45 PM 0
Share

He could be experiencing the same bug as I. As I say, if this is the case it'll throw an error but work unhindered anyway. If it doesn't work at all then it's not the same bug, or even a bug at all.

avatar image Jafar009 · May 28, 2014 at 11:46 PM 0
Share

It's pointing to line 39 in the player script, however I don't know what is causing the error since I don't know what's wrong with my use of GetComponent on the line above.

avatar image meat5000 ♦ · May 29, 2014 at 12:13 AM 0
Share

I copied and pasted an example from the docs and got this error. I figured I was missing something with regard to the Camera not being a regular gameobject.

I tried a lot of different things but in the end cached the Transform of the Camera in Start()

 private var camera$$anonymous$$ain : Camera;
 private var cameraCurrentT : Transform;
 
 function Start()
 {
     camera$$anonymous$$ain = Camera.main;
     cameraCurrentT = camera$$anonymous$$ain.gameObject.GetComponent(Transform);
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Firedan1176 · May 29, 2014 at 12:48 AM

It should say NullReferenceException before that error. This usually means you're basically trying to do something with a variable equal to null. You can try using if(camScript) or if(camScript !=null). hope I help.

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 Jafar009 · May 29, 2014 at 12:55 AM 0
Share

Apologies, could you elaborate on how I would implement this if statement? I'm still new to scripting and am having trouble comprehending.

Thanks for your help!

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

24 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Getcomponent Javascript error 3 Answers

Null Reference Exception on Raycast 2 Answers

[JS] - GetComponents (No appropriate version) 1 Answer

Using arrays and for loop with GetComponent error 1 Answer

I have an error saying its not set to an instance of an object althoug i have set it to an object. Please Help ?? 1 Answer


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