Search Unity

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

Discussion in 'Scripting' started by winterfive, Oct 19, 2018.

  1. winterfive

    winterfive

    Joined:
    May 21, 2018
    Posts:
    31
    Hi,

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

    Code (CSharp):
    1. public void TerminateEnemy()
    2.     {
    3.         _shotObject = _raycastManager.GetCurrentFoundObject();
    4.         StopEnemyActions();
    5.         StartCoroutine(FadeEffect());
    6.     }
    7.    
    8.  
    9.     private void StopEnemyActions()
    10.     {
    11.         // TODO AttackDroneActions is null
    12.         _shotObject.GetComponent<AttackDroneActions>().IsShot = true;
    13.     }
    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!
     
  2. sdk_Narkos

    sdk_Narkos

    Joined:
    Jun 15, 2013
    Posts:
    18
    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();"
     
    winterfive likes this.
  3. SVKsuli

    SVKsuli

    Joined:
    Mar 23, 2014
    Posts:
    62
    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?
     
    winterfive likes this.
  4. winterfive

    winterfive

    Joined:
    May 21, 2018
    Posts:
    31
    The _isShot code looks like this:

    Code (CSharp):
    1. private bool _isShot;
    2.  
    3. public bool IsShot { get; set; }
    The variable is private but my getter and setter are public.
     
  5. winterfive

    winterfive

    Joined:
    May 21, 2018
    Posts:
    31
    In my GameManager class, I have:

    Code (CSharp):
    1.  
    2. private void ShootAtEnemy()
    3. {
    4.     if (OnShoot != null)
    5.     {
    6.         OnShoot();
    7.  
    8.         if (_checkForEnemy.IsEnemy())
    9.         {
    10.             ShootDrone();
    11.         }
    12.     }
    13. }
    14.  
    15.  
    16.  private void ShootDrone()
    17. {
    18.     _effectsManager.TerminateEnemy();
    19.     UpdatePlayerScore();
    20. }
    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.
     
unityunity