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 Flowered · Mar 29, 2015 at 04:57 AM · transformnavmeshnullreferenceexceptionnull

Destroyed Objects causing errors (transform/setdestination)

Hello, after a Long Research I couldn't move on and now I am asking this question...

First off: my Code to find the enemy

 using UnityEngine;
 using System.Collections;
 
 
 public class UndeadFindEnemy : MonoBehaviour
 {
     Transform playereast;
     Transform playereastbuilding;
     NavMeshAgent nav;
 
 
     void Start ()
         {
         playereast = GameObject.FindGameObjectWithTag ("PlayerEast").transform;
         playereastbuilding = GameObject.FindGameObjectWithTag ("PlayerEastBuilding").transform;
         nav = GetComponent <NavMeshAgent> ();
         }
 
 
       private void FixedUpdate ()
         {
 
          if (WithinArea (50.0f, "PlayerEast"))
         {
             if (playereast != null) 
             nav.SetDestination (playereast.position);
         }
 
               else
             {
             if (playereast != null) 
             nav.SetDestination (playereastbuilding.position);
             }
         }
 
 
      private bool WithinArea (float distance = 10.0f, string tag = "PlayerEast")
          {
 
          GameObject[] targets = GameObject.FindGameObjectsWithTag ("PlayerEast");
          var close = false;
          foreach (GameObject target in targets)
                   {
                    close = Vector3.Distance (transform.position, target.transform.position) < distance ? true : false;
                   }
 
                         return close;
 
                 }
 
 }

my Health Codes:

     if(currentHealth <= 0)
     {
         isSinking = true;
         Death ();
     }

     if(isSinking)
     {
         transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);
     }

 void Death ()
 {
     GetComponent <NavMeshAgent> ().enabled = false;
     Destroy (this.gameObject, 2f);
 }



MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. UnityEngine.Transform.get_position ()

"SetDestination" can only be called on an active agent that has been placed on a NavMesh. UnityEngine.NavMeshAgent:SetDestination(Vector3)

When the first object get below 0 life and start sinking the SetDestination error occurs, which is gone after 2 seconds,

but the Transform error is Happening every Frame and my objects won't retarget any other object.

I have no idea what's going on.... ...thank you, Flowered

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 Digital-Phantom · Mar 29, 2015 at 05:23 AM

Once your object starts sinking it technically falls below the navemesh making it impossible for the other objects to track/follow it. But at this time it still exists within the game.

Try using a technique similar to ragdoll. When your object reaches zero health instantiate a copy of it (doesn't need an scripts or any controllers just need to look like the object)

Anyway.. instantiate this copy AND destroy the original at the same time. You won't notice any visual change, your object will still appear to sink and the destroyed object will be removed from the scene.

:)

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 Flowered · Mar 30, 2015 at 04:33 PM 0
Share

Thank you it's working for the SetDestination but I still have my main-issue with the Transform.thing

$$anonymous$$issingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. UnityEngine.Transform.get_position ()

It's causing no error but my Object is not moving anywhere when the targetted Object died(and instantiated the copy which is sinking). it just stand still and will not move to the next object if in range, nor to the building.tag.....? I am confused... thank you

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

Trying to call a function from a dynamically created object 1 Answer

NullReferenceException question. 2 Answers

PlayFab CloudScript NullReferenceException 0 Answers

Finding a RaycastHit tag (Null Reference Exception) 2 Answers

Instantiate gameObject returns null? 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