Change bool value in script on an instance via script in hierarchy

Hi,

I have an EffectsManager script set on an empty object in my hierarchy. At runtime, I call…

public void TerminateEnemy()
    {
        _shotObject = _raycastManager.GetCurrentFoundObject();
        StopEnemyActions();
        StartCoroutine(FadeEffect());
    }
   

    private void StopEnemyActions()
    {
        // TODO AttackDroneActions is null
        _shotObject.GetComponent<AttackDroneActions>().IsShot = true;
    }

Before anyone asks, yes, _shotObject is declared as a class variable.

The AttackDroneActions script is a script placed on a drone prefab. Inside that script, I’ve set up a private bool _isShot with a getter and setter. There are several attack drones that are set to active at runtime (I’m pooling objects). When a player shoots a drone, I’m trying to stop the actions of that particular drone instance. I’ve googled a bit and research shows that the code in StopEnemyActions() should work but it does not as AttackDroneActions shows as null. I am stuck. Help, thanks!

Hello,

The code snippet looks good.

Check if “_shotObject” is already “null” after the call of line 3.
If yes the problem come from “_raycastManager.GetCurrentFoundObject();”

1 Like

Just asking… you write that _isShot is private bool with getter, setter inside AttackDroneActions class. isnt problem that it must be public when you call it from outside?

1 Like

The _isShot code looks like this:

private bool _isShot;

public bool IsShot { get; set; }

The variable is private but my getter and setter are public.

In my GameManager class, I have:

private void ShootAtEnemy()
{
    if (OnShoot != null)
    {
        OnShoot();

        if (_checkForEnemy.IsEnemy())
        {
            ShootDrone();
        }
    }
}


 private void ShootDrone()
{
    _effectsManager.TerminateEnemy();
    UpdatePlayerScore();
}

The _checkForEnemy is a reference to my CheckForEnemy class which checks if the current object found by the player raycast is an enemy object (There’s a few types of enemies). ShootDrone() is only called if the current object is an enemy. ShootDrone() calls TerminateEnemy() in the EffectsManager.