Been trying different ways for a few hours but I don’t know why the space invader enemy isn’t being destroyed. These codes are pre changes I wanted to revert before I went to sleep
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LazerScript : MonoBehaviour
{
public Vector3 projectileMove;
public float projectileTimer;
void Update ()
{
GetComponent<Transform>().position += projectileMove;
projectileTimer += Time.deltaTime;
if (projectileTimer > 1f)
{
Destroy(gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManagerScript : MonoBehaviour
{
public float timer;
public GameObject Enemy;
public GameManagerScript gameManager;
void Update ()
{
timer += Time.deltaTime;
if (timer >= 3)
{
Instantiate( Enemy , GetComponent<Transform>().position , Quaternion.identity );
timer = 0;
}
}
}
The Enemy Invader doesn’t have the Box Collider 2D
so it cannot detect any collisions.
also have you got any more scripts? There is no code in your scripts to destroy the Invader.
This is what it was originally before
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LazerScript : MonoBehaviour
{
public Vector3 projectileMove;
public float projectileTimer;
public GameObject gameManagerObject;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
GetComponent<Transform>().position += projectileMove;
projectileTimer += Time.deltaTime;
if (projectileTimer > 1f)
{
Destroy(gameObject);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Enemy"))
{
// Destroy the enemy object
Destroy(collision.gameObject);
Debug.Log("Enemy Destroyed");
}
}
}
One of the colliders needs to have a Rigidbody2D
component.
Put it on laser3
prefab. // OR put on Invader. If not put on both of them. When I back home I will find out the proper solution.
and Set gravity scale to 0.
Let me know if that works or not
I have recorded the video for you with the solution
Sorry for the overall quality but I am tired and have lots of things to do.