Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Jan 17, 2016 at 04:39 PM by cutjules for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by cutjules · Jan 15, 2016 at 08:08 PM · referenceinstanceexceptionbugs

Help me to debug this error please :"NullReferenceException: Object reference not set to an instance of an object EnemyMovement.Update () (at Assets/Scripts/Enemy/EnemyMovement.cs:30)"

Hello, I've a bug I can't solve myself because I'm too new to Unity. I'm stuck on the Survival Shooter tutorial, on the 7th page (http://unity3d.com/learn/tutorials/projects/survival-shooter/harming-enemies?playlist=17144). When I play the game, the Zombunny keeps walking on the same place and the Player can't be damaged even when I make him contact the Zombunny.

Here are the 2 errors I get in the console (probably the problem is the same ?): NullReferenceException: Object reference not set to an instance of an object EnemyMovement.Update () (at Assets/Scripts/Enemy/EnemyMovement.cs:30)

NullReferenceException: Object reference not set to an instance of an object EnemyAttack.Update () (at Assets/Scripts/Enemy/EnemyAttack.cs:63)

I've copied the code of the script on each page of the tutorial. I've just added extra comments to better understand and remember Unity scripting.

Interesting fact, I've tried these 2 commands just before 30th line in the script EnemyMovement.cs and they returned different results, which sounds suspicious : Debug.Log(enemyHealth == null); returns False Debug.Log(playerHealth == null); returns True So I think the problem is with playerHealth variable, but I don't know what I've to do.

Please, I need help !

alt text Julien

enemymovementbug01.jpg (91.2 kB)
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

  • Sort: 
avatar image
0

Answer by vintar · Jan 15, 2016 at 10:01 PM

Either enemyHealth is null or playerHealth is null. First check if there is a GameObject in the scene with the tag "Player", secondly check if the enemy definitely has a "EnemyHealth" script attached and check if player has "PlayerHealth" script attached. Thirdly, if they do have the respective scripts, check that you are doing a GetComponent on the correct gameObject.

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 cutjules · Jan 16, 2016 at 01:19 AM

Hello Vintar, thanks for your advices. I've checked all the points you're talking about, even if I'm not quite sure about the last one. I don't know how to be sure that the GetComponent is executed on the right GameObject. Here are some screenshots of the points checked, I've made some yellow marks on the interesting details :alt text

As you can see, GameObject Player is tagged "Player" and has its PlayerHealth script

alt text

The GameObject Enemy has its 3 scripts including EnemyHealth


unityplayer01.jpg (78.7 kB)
unityenemy01.jpg (72.7 kB)
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 cutjules · Jan 17, 2016 at 04:38 PM 0
Share

Hello, Just to let you know that I've found the reason of the 2 errors. 2 objects were tagged "Player" in my project : - the main GameObject (converted to prefab) - and the Player's mesh inside the Player GameObject So this instruction was always finding first the Player's mesh : "player = GameObject.FindGameObjectWithTag ("Player").transform;"

Causing this instruction "playerHealth = player.GetComponent ();" to fail. playerHealth was always null because the script PlayerHealth was not attached to the mesh...

I hope my story could help somebody who gets this error message not so precise "NullReferenceException: Object reference not set to an instance of an object "

Follow this Question

Answers Answers and Comments

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

Related Questions

c# Object reference not set to an instance of an object 1 Answer

How to Fix this issue 1 Answer

Reference all instances of a prefab? 1 Answer

Null Reference Exception and not sure where I went wrong? Script and two screenshots attached 0 Answers

CameraController (object reference not set to an instance of an object) 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