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 28, 2018 at 11:09 PM by alphanike97 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by alphanike97 · Jan 28, 2018 at 10:56 PM · errorvector3aidistance

Error with vector3.distance

Hi, could you tell me why the Vector3.Distance function gives me an error of "Object reference not set to an object"

 using UnityEngine;
 using System.Collections;
 using UnityEngine.AI;
 public class IAbasica : MonoBehaviour {
     NavMeshAgent agent;
     GameObject target;
     Animator anim;
     public float Health;
     public float currentDistance;
     public UnityStandardAssets.Characters.FirstPerson.RigidbodyFirstPersonController rb;
     public int DamagePlayerF;
 
     private void FixedUpdate() {
         currentDistance = Vector3.Distance(transform.position, target.transform.position);
 
     }
     void Update () {
 
         anim = GetComponent<Animator>();
 
         agent = GetComponent<NavMeshAgent>();
 
         target = GameObject.FindGameObjectWithTag("Player");
 
         agent.SetDestination(target.transform.position);
 
         if (currentDistance <= 3f) {
             StartCoroutine(DamagePlayer());
         }
 
     }
     IEnumerator DamagePlayer() {
         rb.TotalHealth = rb.CurrentHealth - DamagePlayerF;
         yield return new WaitForSeconds(3f);
     }
 
     public void TakeDamage(float amount){
         Health -= amount;
         if(Health <= 0f)
             Die();
     }
     void Die (){
         Destroy (gameObject);
     }
 }
Comment
Add comment · Show 2
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 Positive7 · Jan 28, 2018 at 11:07 PM 1
Share

Probably "target" is null.

 if (target != null)
     {
          Debug.Log("Yay found target!");
          currentDistance = Vector3.Distance(transform.position, target.transform.position);
     }
     else
     {
          Debug.Log("Target was null!");
     }

But this script has more errors...

avatar image alphanike97 Positive7 · Jan 28, 2018 at 11:08 PM 0
Share

deam right! thx brooo!!!!

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

206 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 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 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 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 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 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

Unexpected symbol 'return' in class, struct, or interface member declaration 3 Answers

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

Unity Vector3.Distance calculating wrong 1 Answer

how to check if an object is betwen enemy and player 1 Answer

Move GameObject to Point A to B 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