- Home /
Question by
sohaybof · Aug 22, 2020 at 10:45 PM ·
transformerror messagenullaccessdestroy object
transform is destroyed (ERROR)... and I want the game to complete
I followed a tutorial to make the enemy follow and identify the player's position but when the player die it give the following error: 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.
I don't know how to fix thanks for your time
there are 2 scripts one for enemy and one for projectile
the enemy script::::
public class enemyCir : MonoBehaviour
{
//عام
public ParticleSystem enemyDeathEffect;
private Transform circle;
//خاص
public GameObject projectile;
private void Start() {
//عام
circle = GameObject.FindWithTag("circle").transform;
}
private void Update() {
//عام
if (Vector2.Distance(transform.position, circle.position) < stoppingDistance){
transform.position = Vector2.MoveTowards(transform.position, circle.position, speed * Time.deltaTime);
}
//عام
private void OnCollisionEnter2D(Collision2D col) {
if (col.gameObject.GetComponent<circlescript>())
{
Destroy(gameObject);
Instantiate(enemyDeathEffect, transform.position, Quaternion.identity);
}
}
}
Comment
Answer by sohaybof · Aug 22, 2020 at 10:46 PM
and this is the projectile script
public class bullet : MonoBehaviour {
private Transform circleTR;
private Vector2 target;
public float speed;
public GameObject destEffect;
private void Start() {
circleTR = GameObject.FindWithTag("circle").transform;
target = new Vector2(circleTR.position.x, circleTR.position.y);
}
private void Update() {
transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
if (transform.position.x == target.x && transform.position.y == target.y){
destroybullet();
}
}
private void OnTriggerEnter2D(Collider2D other) {
if(other.CompareTag("circle")){
Destroy(other.gameObject);
destroybullet();
}
}
void destroybullet(){
Destroy(gameObject);
Instantiate(destEffect, transform.position, Quaternion.identity);
}