Bullet not spawning when clicked

When I click on the trigger


It is showing missing reference exception, i Cant seem to figure it out
this is my bullet code:
using System.Collections;

using System.Collections.Generic;

using System.Security.Cryptography.X509Certificates;

using UnityEngine;

public class bullet : MonoBehaviour

{

[SerializeField] float bulletspeed = 20f;

[SerializeField] float xspeed = 1f;

playermovemet player;

Rigidbody2D myrigidbody;



void Start()

{

    myrigidbody = GetComponent<Rigidbody2D>();

    player = FindObjectOfType<playermovemet>();

    xspeed = player.transform.localScale.x * bulletspeed;

}



void Update()

{

    myrigidbody.velocity = new Vector2(xspeed,0f);

}

 void OnTriggerEnter2D(Collider2D other)

 {

    if(other.tag == "enemy")

    {

        Destroy(other.gameObject);

    }

 }

     void OnCollisionEnter2D(Collision2D other)

      {

        Destroy(gameObject);

    }

   

}

and yes, I also tried this
void OnTriggerEnter2D(Collider2D other)

 {

    if(other.tag == "enemy")

    {

        Destroy(other.gameObject);

    }

    Destroy(gameObject);

 }

Hi, you tagged as an Unreal Question. Have fixed it.

Hi Trigodil,

The error message in your console says that a game object got destroyed. Maybe your component references a bullet game object (in your Hierarchy) instead of a bullet prefab (in your Assets folder)?


See also:

1 Like

Another issue might be that your bullet is getting destroyed because it is colliding with the player’s collider.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms