• 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 cj31387 · Sep 25, 2012 at 12:35 AM · nullexception

Null reference exception:obj ref not set to a instance of obj

This is a common question but I haven't found a solution yet to my script.

I have script b set up like this

 // Global Access
     public static Formation_AI Fai;
 
 public bool isEmptySlot1 = true;  // if the slot is empty it can be filled
     public bool isEmptySlot2 = true;  // if the slot is empty it can be filled
     public bool isEmptySlot3 = true;  // if the slot is empty it can be filled
     public bool isEmptySlot4 = true;  // if the slot is empty it can be filled
 
 above the start function   

I have script A trying to use isEmptySlot1 like this

 void Update () {
         transform.LookAt(playerTarget);
 
         if (Formation_AI.Fai.isEmptySlot1)
         {
             transform.position = Vector3.Lerp(transform.position, FormTarg3.position, Time.deltaTime * smoothTranslateSpeed);     
         }

I get a null reference exception on the if statement. I don't know what is going wrong because i made public static Formation_AI Fai; in my other script and its set to a object that is in the scene. Well the object is a prefab of a root empty game object and 4 child empty game objects. But the script is on the root of the prefab.

// Current Problem -------------------------------------------// Well the prefab IS in the scene already (the fighters are supposed to move to the prefab slots in the scene), and it still gives me a null reference error. With getcomponent.

Ok the problem was I needed to set it in the inspector with a prefab with Formation_AI on it.

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

1 Reply

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

Answer by hathol · Sep 25, 2012 at 10:16 AM

I think the prefab part is your problem. You need to create an instance of the object first before you can access it's non-static members. What exactly are you trying to do there? If you know you'll only ever need one instance of the object, have a look at the singleton pattern. Alternatively, just make the bools static as well and access them directly. Exposing a single instance of an object like that is usually not a good idea and tends to causes bad things to happen.

Comment
Add comment · Show 8 · 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 cj31387 · Sep 25, 2012 at 10:27 AM 0
Share

There will be more than one of that prefab in the scene later on. How do I create a instance of the object? The prefab is already in the scene though. What I'm trying to do is access the variables from my formation script inside my Fighter_AI script.

avatar image hathol · Sep 25, 2012 at 10:43 AM 1
Share

A) You should really get rid of that global access stuff, especially if you're going to have multiple instances of that object at some point. See if you can give your Fighter object a reference to the desired Formation_AI instance directly ins$$anonymous$$d.

B) I still suspect your static variable holds only a reference to the object prefab and not to the instance in the scene. So as a short term solution: In you Formation_AI add

 void Start()
 {
     Fai = this;
 } 
avatar image refardeon · Sep 25, 2012 at 11:08 AM 1
Share

Check out the $$anonymous$$anual here: http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

That should get you going.

avatar image refardeon · Sep 25, 2012 at 11:25 AM 1
Share

Yes, but like hathol said: If you use a static reference like this, you can't access more than one instance of this script. From what you wrote, I suspect that's not what you want. The problem is: $$anonymous$$ultiple instances of your script will all overwrite the static reference Fai with themselves - so in the end, only one of these scripts will be accessible by the global variable.

About GetComponent: You need an object with the Component attached somewhere where you can access it. You could also use GameObject.FindObjectOfType(); however, this is computationally expensive - don't use it every frame!. If you only need to assign the formations at the start of the game, it should suffice.

avatar image refardeon · Sep 25, 2012 at 11:37 AM 1
Share

Yes, if the formation prefab is actually instantiated the scene. If it's only in the project folder, GetComponent will fail.

Show more comments

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

11 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

Related Questions

Null Reference Exception After refering to an Array 1 Answer

NullRefernceExcepception with Raycasting 1 Answer

NullReferenceException - With Network PlayerSpawn 0 Answers

Null reference Exception - but it's all there 1 Answer

Strange Xcode Error for iOS? YS Bundle Null Exception 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