• 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 Raymond 2 · Dec 03, 2011 at 06:31 PM · nullreferenceexception

NullReferenceException Object reference not set to an instance of an object

i got thios error message in one of my script, and i can't figure out why it shows that error, anybody who can help?

this is the full error message: NullReferenceException: Object reference not set to an instance of an object followPlayer.Update () (at Assets/Scripts/SeaSerpent/followPlayer.js:14)

and here's the code

 var Target : Transform ;
 
 var maxDistance = 1;
 
 var Speed = 7;
 
 var getStart :flyscript3;
 getStart = GetComponent(flyscript3);
 
 function Update () 
 {
     var dist = Vector3.Distance(Target.position, transform.position);
     

//line 14 if(getStart.start) {

     if(dist > maxDistance)
         {
         transform.LookAt(Target);
         transform.Translate(Vector3.forward * Speed * Time.smoothDeltaTime);
         }
         else
         {
             if(dist < maxDistance)
             {
                 transform.LookAt(Target);
             }
         }
     }
     else
     {
     
     }
 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by dannyskim · Dec 03, 2011 at 11:20 PM

Don't believe you can initialize the getStart value outside of a method. What you should try doing is removing that GetComponent line from the variable declarations and adding it into either the Start() or Awake() method, and see what happens.

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

Answer by jahroy · Dec 03, 2011 at 07:51 PM

You're getting that error because the variable named getStart is null.

You should try to assign that variable in Start() or Awake().

You should also check if it's null (and print an error message) before you try to use it:

function Start () { getStart = gameObject.GetComponent(flyscript3); }

/ do nothing if getStart is null /

function Update () { / if getStart is null, print error message and bail! /

 if ( ! getStart ) {
     Debug.LogError("getStart is null!");
     return;
 }

 if ( getStart.start ) {
     /* do stuff */
 }

}

Comment
Add comment · Show 3 · 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 Raymond 2 · Dec 04, 2011 at 12:48 AM 0
Share

I still get the same message, could it be something else?

avatar image syclamoth · Dec 04, 2011 at 01:41 AM 0
Share

No, it really couldn't be. Assu$$anonymous$$g that 'getStart.start' is a boolean, boolean values can never be null (only true or false) and so it can't be that. For the checker, you could try the line

 if(getStart == null)

ins$$anonymous$$d- sometimes the other thing doesn't work as expected (but I can't remember the circumstances under which that happens).

avatar image jahroy · Dec 04, 2011 at 04:23 AM 1
Share

You might also get that error if there isn't a flyscript3 script attached to the object your script is attached to, but you still want to call GetComponent from Start or Awake.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

NullReferenceExeption: Object reference not set to an instance of an object. 1 Answer

NullReferenceException: Object reference not set to an instance of an object - error in AdvancedBuildingSystem! 0 Answers

NullReferenceException: I think unity bug 3 Answers

RaycastHit2D not interacting with tags 1 Answer

Can no longer create pre-fab 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