Search code examples
unity-game-engineinstantiation

How to reference an Instantiated object in Unity?


I have a weapon that I want to instantiate an object (In this case a bullet), then wait until that bullet has hit an object before the weapon is allowed to instantiate another object.

I am currently doing this by having this on the weapon script:

public class weaponScript : MonoBehaviour
{
        public gameobject projectilePrefab;
        public bool launchable;

        void Update{
        if(launchable){
             Instantiate(projectilePrefab, firePoint.transform.position, transform.rotation);
             launchable = false;
        }
    }

And this on the bullet script:

public class projectile : MonoBehaviour
{
    void OnCollisionEnter2D(Collision2D other){
        weaponScript.launchable = true;
    }
}

This would work perfect for my needs, however this doesn't work because the projectile has no definition of what weaponScript is, therefore it can't set the launchable variable on the weaponScript to true.

I could use the FindObjectOdType() function, and that works when you have one weapon in the scene, but once you have more than one weaponScript in the scene at a time, you start to having problems determining who is who.

Is there a way for the weaponScript to set itself as a variable when it instantiates an object so it would be like:

public class weaponScript : MonoBehaviour
{
        public gameobject projectilePrefab;
        public bool launchable;

        void Update{
        if(launchable){
             Instantiate(projectilePrefab, firePoint.transform.position, transform.rotation);
             [InstanciatedObjectHere].parentWeapon = this.gameobject;
             launchable = false;
        }
    }

That way all the projectile has to do is:

public class projectile : MonoBehaviour
{
    void OnCollisionEnter2D(Collision2D other){
        parentWeapon.launchable = true;
    }
}

Solution:

I know Pastebin won't keep the script forever, so I'm going to put the answer here as an edit: (plus it makes it easier for some else who might stumble upon this to read)

I followed Peridis's answer, but it didn't work immediately, so I ended up tweaking it and came up with this:

public class weaponScript : MonoBehaviour
{
        public gameobject projectilePrefab;
        public bool launchable;
        private projectile projectileScript;

        void Update{
        if(launchable){
            GameObject projectileIntantiated = Instantiate(projectilePrefab, firePoint.transform.position, transform.rotation);
            projectileScript = projectileIntantiated.GetComponent<projectile>();
            projectileScript.parentWeapon = this.gameObject.GetComponent<weaponScript>();
            launchable = false;
        }
    }




public class projectile : MonoBehaviour
{
    public weaponScript parentWeapon;

    void OnCollisionEnter2D(Collision2D other){
        parentWeapon.launchable = true;
    }
}

Thank you for the help Peridis!


Solution

  • You can create a variable of type GameObject when Instantiating the projectile

    GameObject projectileIntantiated = Instantiate(projectilePrefab, firePoint.transform.position, transform.rotation);
    projectileIntantiated.GetComponent<projectile>().parentWeapon = this.gameobject;