- Home /
(in the end of the code...) when i try to run the program that tells me that the object is destoryed and i cant access it someone help?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Bullet : MonoBehaviour {
 private Transform target;
 public float speed = 70f;
 public GameObject impactEffect;
 public void Seek (Transform _target)
 {
     Debug.Log("seek");
     target = _target;
 }
   
 void Update()
 {
     if (target == null)
     {
         Debug.Log("destroy");
         // Destroy(gameObject);
         return;
     }
     Debug.Log(target);
     Vector3 dir = target.position - transform.position;
     float distanceThisFrame = speed * Time.deltaTime;
     if (dir.magnitude <= distanceThisFrame)
     {
         HitTarget();
         return;
     }
     transform.Translate(dir.normalized * distanceThisFrame, Space.World);
     transform.LookAt(target);
 }
 void HitTarget()
 {
     // GameObject effectIns = Instantiate(impactEffect, transform.position, transform.rotation);
     // Destroy(effectIns, 1f);
     // Debug.Log(target.gameObject);
     // if (gameObject != null) {
         
     //     Destroy(target.gameObject);
     // }
     Destroy(gameObject);
     
 }
}
,using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Bullet : MonoBehaviour {
 private Transform target;
 public float speed = 70f;
 public GameObject impactEffect;
 public void Seek (Transform _target)
 {
     Debug.Log("seek");
     target = _target;
 }
   
 void Update()
 {
     if (target == null)
     {
         Debug.Log("destroy");
         // Destroy(gameObject);
         return;
     }
     Debug.Log(target);
     Vector3 dir = target.position - transform.position;
     float distanceThisFrame = speed * Time.deltaTime;
     if (dir.magnitude <= distanceThisFrame)
     {
         HitTarget();
         return;
     }
     transform.Translate(dir.normalized * distanceThisFrame, Space.World);
     transform.LookAt(target);
 }
 void HitTarget()
 {
     // GameObject effectIns = Instantiate(impactEffect, transform.position, transform.rotation);
     // Destroy(effectIns, 1f);
     // Debug.Log(target.gameObject);
     // if (gameObject != null) {
         
     //     Destroy(target.gameObject);
     // }
     Destroy(gameObject);
     
 }
}
               Comment
              
 
               
              Answer by logicandchaos · Jan 26, 2021 at 12:34 PM
if you uncomment this
 if (gameObject != null) 
 {         
      Destroy(target.gameObject);
 }
and comment this Destroy(gameObject); then you shouldn't get that error.
Your answer
 
 
             Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Destruct gameobject by call from another script 1 Answer
How to turn object into a particle system and back? 0 Answers
2D C# destroy a GameObject on collision 2 Answers
How to change the pivot point of a 3d game object in unity 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                