Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 daviddickball · Jul 07, 2015 at 10:55 PM · instanceobject reference

Object reference not set to instance of an object - when it is!

I've had this error many times, and always when the object can't be found. But this time the object IS found, and the script thrown an error. But it's not a fatal error and the game plays just as it should. The same script actually makes use of the object which it's complaining about, and it's fine. I can't see a problem, or why I'm getting this error.

Here's my code. I'm getting a child object of the object the script is attached to (there are others in the game with the same name), hence why I use transform.Find() and not GameObject.Find()

 private var fireFrom: GameObject;

 function Start() {
     fireFrom = transform.Find("FireFrom").gameObject;
 }
Comment
Add comment · Show 4
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 YoungDeveloper · Jul 07, 2015 at 11:05 PM 1
Share

show us your hierarchy and where the script is assigned

avatar image RLin · Jul 07, 2015 at 11:57 PM 0
Share

Why don't you just use Gameobject.find?

avatar image daviddickball · Jul 08, 2015 at 09:26 AM 0
Share

Normally I would do that @RLin, but in this case I need to access a child object. I use transform.Find("FireFrom").gameObject elsewhere to find a child object, and it doesn't thrown an error. Is there a better way to find a child object maybe?

avatar image daviddickball · Jul 08, 2015 at 09:28 AM 0
Share

@YoungDeveloper I have "Turret", inside that is "TurretHead" (where this script is), and inside that is "FireFrom".

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Zambii · Jul 08, 2015 at 09:10 AM

I think the problem is that unity does not know specifically which one you want to use the transform since you are telling unity to find any transform that is attached to a game object called FireFrom and since there are more then one in the scene with the same name, unity gets confused. I don't know what you are trying to do with that transform, but it seems to me like that is where you want to instantiate a projectile from, so one simple way to fix it would be to make the transform be public and set the transform directly from within unity. In C# it would be something like this:

public Transform fireFrom;

//And that is all since you will be manually selecting the transform from within unity.

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
1

Answer by ransomink · Jul 08, 2015 at 09:56 PM

Make sure "FireFrom" is spelled the same inside the hierarchy and the script. Also check that the script is attached to the TurretHead gameobject (just as a formality). I would throw an debug exception to see if the FireFrom gameobject comes back as null.

 fireFrom = transform.Find("FireFrom").gameObject;
 
 if (fireFrom == null)
     Debug.LogError("No child with the name 'FireFrom' attached to TurretHead.");
Comment
Add comment · Show 2 · 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 Bonfire-Boy · Jul 08, 2015 at 11:01 PM 0
Share

Errr, make sure that 'FireForm' is spelled the same as 'FireFrom'? ;)

But yes, I'd do both of those things too.

avatar image Bonfire-Boy · Jul 08, 2015 at 11:04 PM 0
Share

Other things that might be worth looking for...

The object being destroyed after the reference is cached.

There being multiple GameObjects called 'FireFrom'.

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

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

Related Questions

Gameobject.find & my error NullReferenceException: Object reference not set to an instance of an object 1 Answer

Pause Script Suddenly Stops Working 0 Answers

NullReferenceException Issues 3 Answers

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

"Object reference not set to an instance of an object" Error 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