How do i reference instantiated object in another script?

I had instantiated object named bullet on Shoot() that i want to be referenced in another script, so i can see the distance between the bullet and the enemy. And if the distance is far enough the enemy can dash away from the bullet. Any idea how can i do that?

public class Shooting : MonoBehaviour
{
    //Titik awal projectile di tembakkan
    public Transform firePoint;

    //Prefab untuk projectile
    public GameObject bulletPrefab;

    public float bulletForce = 20f;
    public int damage = 20;


    //Method yang akan dipanggil Input System saat tombol kiri mouse ditekan
    public void onShooting(InputAction.CallbackContext context)
    {
        if (context.started)
        {
            Shoot();
        }
    }

    //Method yang dipanggil pada onShooting
    public void Shoot()
    {
        //Var bullet untuk projectile yang akan ditembakkan. Mengambil prefab, dan menggunakan data lokasi beserta rotasi dari firePoint
        GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);

        //Mengambil rigidbody dari projectile yg ditembakkan
        Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();

        //Mengatur arah bergerakknya projectile yang ditembakkan
        rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
    }
}

There are many methods, a simple way to be to store the bullet as a variable in the Shooting behaviour, and reference your shooting behavior.

public class Shooting : MonoBehaviour
{
    //Titik awal projectile di tembakkan
    public Transform firePoint;

    public GameObject lastBullet;

    //Prefab untuk projectile
    public GameObject bulletPrefab;

    public float bulletForce = 20f;
    public int damage = 20;


    //Method yang akan dipanggil Input System saat tombol kiri mouse ditekan
    public void onShooting(InputAction.CallbackContext context)
    {
        if (context.started)
        {
            Shoot();
        }
    }

    //Method yang dipanggil pada onShooting
    public void Shoot()
    {
        //Var bullet untuk projectile yang akan ditembakkan. Mengambil prefab, dan menggunakan data lokasi beserta rotasi dari firePoint
        lastBullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);

        //Mengambil rigidbody dari projectile yg ditembakkan
        Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();

        //Mengatur arah bergerakknya projectile yang ditembakkan
        rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
    }
}

Then in another script

public Shooting shooting;

var bullet = shooting.LastBullet;

Although you would have to check for null checks!

Alternatively you can use a list to store many bullets.

public class Shooting : MonoBehaviour
{
    //Titik awal projectile di tembakkan
    public Transform firePoint;

    public List<GameObject> bullets;

    //Prefab untuk projectile
    public GameObject bulletPrefab;

    public float bulletForce = 20f;
    public int damage = 20;


    //Method yang akan dipanggil Input System saat tombol kiri mouse ditekan
    public void onShooting(InputAction.CallbackContext context)
    {
        if (context.started)
        {
            Shoot();
        }
    }

    //Method yang dipanggil pada onShooting
    public void Shoot()
    {
        //Var bullet untuk projectile yang akan ditembakkan. Mengambil prefab, dan menggunakan data lokasi beserta rotasi dari firePoint
        bullets.Add(Instantiate(bulletPrefab, firePoint.position, firePoint.rotation));

        //Mengambil rigidbody dari projectile yg ditembakkan
        Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();

        //Mengatur arah bergerakknya projectile yang ditembakkan
        rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
    }
}
1 Like