• 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 Kyouri · Nov 05, 2010 at 08:57 PM · objectreference

Stupid question... Object reference not set to an instance of an object

I'm really not sure why I'm getting this error, it's a very short script and I can't find a single thing wrong with it. I get a new instance of this error every single frame. The code works fine though, which is even more baffling.

void Update () {
    if (this.GetComponent<BaseStatFunctions>().isPicked == false)
    {
        this.transform.position = new Vector3(transform.position.x, transform.position.y, 0);
        this.transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z);
    }
    else if (this.GetComponent<BaseStatFunctions>().isPicked == true)
    {
        GameObject ship = GameObject.Find("Ship");
        this.transform.rotation = ship.transform.rotation;
    }
}

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
Best Answer

Answer by denewbie · Nov 06, 2010 at 04:23 PM

try this and look at what the console prints out

void Update () {

 BaseStatFunctions myScript = GetComponent&lt;BaseStatFunctions&gt;();

 if (myScript ) // Checks if your script is null

{ if ( !myScript.isPicked) { transform.position = new Vector3(transform.position.x, transform.position.y, 0); transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z); } else { GameObject ship = GameObject.Find("Ship"); if (ship) // Checks if your ship exits transform.rotation = ship.transform.rotation; else print("ship does not exits");

      }
 }
 else
     print("Script does not exits");

}

The point its to check if the objects you're using exist.in this part the 2 objects would be: 1. the "GetComponent()" If this was missing then you might have forgotten to attach this script to the SAME GameObject. if you're calling it from another game object you should use this: GameObject.Find( "name of the gameObject" ).GetComponent();

  1. the "ship" If this was missing then you dont have a game object named "Ship" in your scene.
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 Kyouri · Nov 07, 2010 at 11:54 PM 0
Share

This solved it, with a little extra debugging I found the object causing the error, this script was accidentally attached to my camera as well as every other object. It didn't have a stats script. Thanks!

avatar image
1

Answer by IJM · Nov 05, 2010 at 10:36 PM

Do you have a GameObject in the scene named "Ship"?

GameObject ship = GameObject.Find("Ship");

That could be it.

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 Kyouri · Nov 07, 2010 at 10:57 PM 0
Share

I do, it never leaves the scene either, there is always a ship as long as anything is on screen.

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

No one has followed this question yet.

Related Questions

Null reference.. 2 Answers

"Object reference not set to an instance of an object" error 1 Answer

Object Pool object reference is null? 1 Answer

C# transfer reference of object 2 Answers

Object reference not set help 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