Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 hugodeheld · Jul 03, 2015 at 01:57 PM · attackzombiereference-other-object

Attack player script not working?

i am making a zombie game, but i keep getting an error about an object not set, and wont add the damage to the player. her is the error:

NullReferenceException: Object reference not set to an instance of an object ZombieAttack.Update () (at Assets/Scripts/Zombie/ZombieAttack.cs:22)

but it is assigned and working like it should, exept for the health going down.

here is the code attached to the player:

 using UnityEngine;
 using System.Collections;
 
 public class ZombieAttack : MonoBehaviour
 {
     public GameObject[] enemies;
     LifeScript life;
     void Start()
     {
         life = GetComponent<LifeScript>();
         enemies = GameObject.FindGameObjectsWithTag("zombie");
     }
     
     void Update()
     {
         enemies = GameObject.FindGameObjectsWithTag("zombie");
         for (int i = 0; i < enemies.Length; i++)
         {
             Debug.Log(i);
             if (enemies[i].GetComponent<ZombieWalking>().canAttack == true)
             {
                 life.health -= enemies[i].GetComponent<ZombieWalking>().dam;
                 enemies[i].GetComponent<ZombieWalking>().canAttack = false;
             }
             
         }
     }
 }

and here is the code the script is referencing on the zombie:

 using UnityEngine;
 using System.Collections;
 
 public class ZombieWalking : MonoBehaviour {
 
     public float speed;
     public GameObject player;
     public float distanceToPlayer;
     public float cooldown;
     public bool canAttack = false;
     public int dam = 6;
     // Use this for initialization
     void Start () {
         player = GameObject.FindGameObjectWithTag ("rick");
     }
     
     // Update is called once per frame
     void Update () {
         distanceToPlayer = Vector3.Distance (transform.position, player.transform.position);
         if (distanceToPlayer > 1.6f && distanceToPlayer < 10.0f) {
             transform.position = Vector3.MoveTowards (transform.position, player.transform.position, speed);
             transform.LookAt(player.transform.position);
         }
         if (distanceToPlayer < 2)
         {
             if (!canAttack)
             {
                 cooldown -= Time.deltaTime;
                 if (cooldown < 0.1f)
                 {
                     canAttack = true;
                     cooldown = 3;
                 }
             }
         }
         if (distanceToPlayer > 2)
         {
             canAttack = false;
             cooldown = 3;
         }
     }
 }

please help if you can :(

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

Answer by malkere · Jul 03, 2015 at 02:24 PM

null reference means its null. at 22 that means your player is null. that means your find tag is not working. Should it be "Rick" and not "rick"? or something like that. add a debug before the look at to see what player.gameObject.name returns as (should be null) then pause and look at the player during runtime to see what his tag looks like. should be a silly mistake you'll notice if you look closer.

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 hugodeheld · Jul 03, 2015 at 04:34 PM 1
Share

one of the script names changed, and i forgot to change it in the scripts. still allot of thanks for helping, i did find a wrong tag.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Zombie script 1 Answer

how to make an AI damage a third person controller ? 1 Answer

How do I get my zombie, to attack my 3rd person controller? 0 Answers

Zombie Alien Mellee Attack 1 Answer

Attack Script problem please help 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