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 /
avatar image
0
Question by Stiliatis Togrou · Jun 30, 2016 at 02:29 PM · error3dtutorialobject reference

Survival Shooter game object reference issue

Hello. i am currently watching the survival shooter tutorial and i was doing fine. I am following precisely the instruction and besides the fact that i am kinda new in unity , i am a bit familiar with some stuff because i already made some 2D games(with Unity). So the issue here is that the code and game was perfectly working besides some details in the animator which i fixed. I run the game and it was warking just fine. Then suddenly without doing anything, i run the game agan and it wasn't working... the enemy was chasing but did nothing else... the error was :

NullReferenceException: Object reference not set to an instance of an object CompleteProject.

which basically is weird because i triple checked it for hours. all the references and objects and tags and all of the stuff are fine. The code is copied from the tutorial. I feel like it's a bug. But i really need a second "eye" on that to make sure it's not an issue from my position.

**Error is about playerHealth.currentHealth on the if statement. I also searched for answers and i someone suggested to Debug the playerHealth variable to see if it's null (playerHealth = player.GetComponent ();). And it's actually null. But i dont understand why

The code :

 using UnityEngine;
 using System.Collections;
 
 namespace CompleteProject
 {
     public class EnemyAttack : MonoBehaviour
     {
         public float timeBetweenAttacks = 0.5f;     // The time in seconds between each attack.
         public int attackDamage = 10;               // The amount of health taken away per attack.
 
 
         Animator anim;                              // Reference to the animator component.
         GameObject player;                          // Reference to the player GameObject.
         PlayerHealth playerHealth;                  // Reference to the player's health.
         EnemyHealth enemyHealth;                    // Reference to this enemy's health.
         bool playerInRange;                         // Whether player is within the trigger collider and can be attacked.
         float timer;                                // Timer for counting up to the next attack.
 
 
         void Awake ()
         {
             // Setting up the references.
             player = GameObject.FindGameObjectWithTag ("Player");
             playerHealth = player.GetComponent <PlayerHealth> ();
 
             if (playerHealth == null)
             {
                 Debug.LogError("Is Null"); // this code is execute. playerHealth is actually null
             }
 
             enemyHealth = GetComponent<EnemyHealth>();
             anim = GetComponent <Animator> ();
         }
 
 
         void OnTriggerEnter (Collider other)
         {
             // If the entering collider is the player...
             if(other.gameObject == player)
             {
                 // ... the player is in range.
                 playerInRange = true;
             }
         }
 
 
         void OnTriggerExit (Collider other)
         {
             // If the exiting collider is the player...
             if(other.gameObject == player)
             {
                 // ... the player is no longer in range.
                 playerInRange = false;
             }
         }
 
 
         void Update ()
         {
             // Add the time since Update was last called to the timer.
             timer += Time.deltaTime;
 
             // If the timer exceeds the time between attacks, the player is in range and this enemy is alive...
             if(timer >= timeBetweenAttacks && playerInRange && enemyHealth.currentHealth > 0)
             {
                 // ... attack.
                 Attack ();
             }
 
             // If the player has zero or less health...
             if(playerHealth.currentHealth <= 0) // object reference error here
             {
                 // ... tell the animator the player is dead.
                 anim.SetTrigger ("PlayerDead");
             }
         }
 
 
         void Attack ()
         {
             // Reset the timer.
             timer = 0f;
 
             // If the player has health to lose...
             if(playerHealth.currentHealth > 0) // object reference error here
             {
                 // ... damage the player.
                 playerHealth.TakeDamage (attackDamage);
             }
         }
     }
 }

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 Hellium · Jun 29, 2016 at 07:47 PM 0
Share

Are you sure you have only one gameobject with the Player tag ? Are you sure this objects holds the PlayerHealth script ?

avatar image Stiliatis Togrou Hellium · Jun 29, 2016 at 10:15 PM 0
Share

Yes. I checked it and the Player object is the only one with the tag "Player" and also it holds the PlayerHealth script. I have a script that finds object by tag and moves towards it. and it works jsut fine. It's really weird. i dont know if i can call it a bug

avatar image Whiteleaf · Jun 30, 2016 at 04:07 PM 0
Share

I'd also check if the player game object itself is null.

This randomly happens to me as well. An object will randomly start going null without any changes to the script. I don't really have a fix for it, as it usually just fixes itself.

avatar image franklx2 · Jul 18, 2016 at 06:16 AM 0
Share

Were you able to find a solution to this issue? I'm stuck on the same bug

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by bethcam · Oct 25, 2016 at 04:26 PM

I think the issue here is with the namespace under which your classes/scripts are declared.

Your EnemyAttack class is declared under the CompleteProject namespace (line 4 of your code) so I'm guessing it's located in the Assets\ _CompletedAssets\Scripts\Player folder. Since you're following the tutorial, it's likely that at least some of the scripts you attached to gameobjects in the scene (like the PlayerHealth script attached to the Player gameobject) are not declared under the CompleteProject namespace and are located in the Assets\Scripts\Player folder rather than the Assets\ _CompletedAssets\Scripts\Player folder.

What you need to do is to make sure that you're referencing the right scripts in the right namespace.

So in this case, make sure that the PlayerHealth script attached to the Player gameobject that you're trying to reference is also declared under the CompleteProject namespace (like your EnemyAttack script), or else just remove the CompleteProject namespace declaration (and the corresponding braces) from your EnemyAttack script.

As a side note, in case you ever need to access a scripts declared under another namespace, you can reference it specifically by adding the namespace and a dot in front of the class name. E.g., if you're in a script which is not declared under the CompleteProject namespace but you want to reference the EnemyMovement script declared under the CompleteProject namespace, you can write CompletedProject.EnemyMovement instead of just EnemyMovement.

Hope that makes sense! :)

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 rickbadan · Feb 20, 2018 at 03:21 AM 0
Share

Worked here, thanks!

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity 2019.4.14f_4037e52648cd error while play a game. 0 Answers

An object reference is required to access non-static member (CS0120) 1 Answer

3D Game Kit Textures Not Importing 0 Answers

Broken Tutorial Files 2 Answers

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