Instantiate at Shooter

Rick was placing the shooter script on the defender, and adding an empty game object called Gun as a child object.
My approach was to still create the child object called gun, but to place the shooter script on the Gun.
Then when I call Fire it calls Fire from getting the component from the child object.

So on my defender I have the following relevant code:

Shooter shooter = null;

// Start is called before the first frame update
    void Start()
    {        
        shooter = GetComponentInChildren<Shooter>();
        if (shooter == null)
        {
            Debug.Log("Shooter component is null on " + gameObject.name);
        }
    }

private void Fire()
    {
        shooter.Fire();
    }

For me it makes more sense that it’s the gun that shoots/fires so the projectile should be attached/slotted in at the Gun child object level.

Privacy & Terms