• 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 akvastudio1232 · Jan 21 at 04:05 PM · scripting problemscript error

MissingReferenceException: The object of type 'Transform'

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 () (at :0) Enemy.Pursueplayer () (at Assets/Scripts/Enemy.cs:47) Enemy.Update () (at Assets/Scripts/Enemy.cs:41)

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;

 public class Enemy : MonoBehaviour
 {
     [Header("Enemy Health and Damage")]
     public float giveDamage = 5f;
     public float enemySpeed;
 
     [Header("Enemy Things")]
     public NavMeshAgent enemyAgent;
     public Transform LookPoint;
     public GameObject ShootingRaycastArea;
     public Transform playerBody;
     public LayerMask PlayerLayer;
 
 
     [Header("Enemy Shooting Var")]
     public float timebtwshoot;
     bool previouslyShoot;
 
     [Header("Enemy States")]
     public float visionRadius;
     public float shootingRadius;
     public bool playerInvisionRadius;
     public bool playerInshootingRadius;
     public bool isPlayer = false;
 
     private void Awake()
     {
         enemyAgent = GetComponent<NavMeshAgent>();
     }
 
     private void Update()
     {
         playerInshootingRadius = Physics.CheckSphere(transform.position, visionRadius, PlayerLayer);
         playerInshootingRadius = Physics.CheckSphere(transform.position, shootingRadius, PlayerLayer);
 
         if(playerInvisionRadius && !playerInshootingRadius) Pursueplayer();
         if(playerInshootingRadius && playerInshootingRadius) ShootPlayer();
     }
 
     private void Pursueplayer()
     {
         if(enemyAgent.SetDestination(playerBody.position))
         {
             //animations
         }
     }
 
     private void ShootPlayer()
     {
         enemyAgent.SetDestination(transform.position);
 
         transform.LookAt(LookPoint);
 
         if (!previouslyShoot)
         {
             RaycastHit hit;
 
             if (Physics.Raycast(ShootingRaycastArea.transform.position, ShootingRaycastArea.transform.forward, out hit, shootingRadius))
             {
                 Debug.Log("Shooting" + hit.transform.name);
 
                 PlayerScript playerBody = hit.transform.GetComponent<PlayerScript>();
 
                 if(playerBody != null)
                 {
                     playerBody.playerHitDamage(giveDamage);
                 }
 
 
             }
 
             previouslyShoot = true;
             Invoke(nameof(ActiveShooting), timebtwshoot);
         }
     }
 
      private void ActiveShooting()
     {
         previouslyShoot = false;
     }
 
 }
 
 
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

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by AshwinTheGammer · Jan 21 at 05:17 PM

The error message is indicating that the script is trying to access the "position" property of a "Transform" object that has been destroyed. This is likely happening in the "PursuePlayer()" and "ShootPlayer()" methods, where the script is trying to access the "playerBody" transform and "LookPoint" transform, respectively. It seems like you are trying to access the playerBody's position and look at LookPoint but both of these objects may be destroyed and you have no reference to them. You should check the object is not null before trying to access it. You should add null check like if(playerBody!=null) before accessing the player's position and if(LookPoint!=null) before trying to look at it.

ACCEPT IT AS AN ANSWER.

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 akvastudio1232 · Jan 25 at 03:43 AM

Can you make the script as you say

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

JSON file working for PC but not android? 1 Answer

Movement Script stops working 0 Answers

Object flashing in and out every frame (because of my script) 0 Answers

"StateMachineBehaviour" Not working for sub sates ? 0 Answers

The script don't inherit a native class that can manage a script. 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