• 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
-1
Question by Gooey-Man · Mar 07, 2014 at 01:21 AM · referenceexception

NullRefrenceException: Object reference not set to an instance of an object?

I get an error saying Object reference not set to an instance of an object Test..ctor () at (Assets/Test.js:4). What does it mean and how do I fix it? Here is my script...

 var damping:int=2;
 var target:GameObject;
  
 var lookPos = target.transform.position - transform.position;
   lookPos.y = 0;
   var rotation = Quaternion.LookRotation(lookPos);
   transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
 
Comment
Add comment · Show 2
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 Kiloblargh · Mar 10, 2014 at 07:22 AM 0
Share

It means that you don't know how to:

  1. write a script

  2. use the Inspector panel

  3. read a reference manual

  4. use a search engine

You could fix it by learning to do one or more of the above, but honestly, you should probably just give up.

avatar image wibble82 · Mar 10, 2014 at 09:12 AM 1
Share

This forum is for people to ask questions about problems they are confused with, whether an experienced coder or a total beginner. $$anonymous$$iloblargh, if you don't like beginner questions don't read them. Really winds me up when obstinate people put down beginners because they're stuck on simple problems.

Gooey-$$anonymous$$an, that error means that you are attempting to access an object that hasn't be set to reference anything (or the thing its referencing has been deleted). From your code, that'll be the 'target' on line 4. If you add Debug.Log(target); on the line above it'll probably tell you this.

To fix it, you need to set the target to something. Whether that should be in the inspector panel or somewhere else depends on your game.

And don't listen to idiots like $$anonymous$$iloblargh - we all had to learn once :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by william9518 · Mar 07, 2014 at 01:26 AM

First, put your code that isn't declaring a variable inside a function, by declaring function Update() or function Start() or any other function you want. Second, you have to assign the target a GameObject via the Unity Editor. The way you do this is by attaching the script to a gameObject, then dragging the target into the empty slot in the target variable in the inspector.

Comment
Add comment · Show 4 · 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 Gooey-Man · Mar 07, 2014 at 01:44 AM 0
Share

Ok I will try it.

avatar image Gooey-Man · Mar 07, 2014 at 01:47 AM 0
Share

Wow Thanks I have tried so many times even with functions and it still didn't work how?

avatar image Gooey-Man · Mar 07, 2014 at 01:48 AM 0
Share

I also still attached the game object to the script but it still didn't work... ?

avatar image Berenger · Mar 10, 2014 at 09:33 AM 0
Share

If it's working now, feel free to upvote william and mark the question as answered ;)

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

23 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

Related Questions

NullReferenceException Error 6 Answers

Instantiate() as GameObject = null reference 1 Answer

Unassigned Reference Exception ? 0 Answers

Null Reference exception error 2 Answers

Why is this code getting NullReferenceException 2 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