Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Using an array of GameObjects to define a variable.

Discussion in 'Scripting' started by ThickyVEVO, Oct 2, 2022.

  1. ThickyVEVO

    ThickyVEVO

    Joined:
    May 28, 2020
    Posts:
    16
    I have a list of targets, that when their health float reaches zero, deletes itself and sends an event to which my "Gun" script subscribes.


    public void Damage(float damage)
    {
    health -= damage;
    if (health <= 0)
    {
    OnEnemyKilled?.Invoke(this, EventArgs.Empty);

    source.PlayOneShot(kill, 0.25f);

    Destroy(gameObject);
    }
    }



    private void Target_OnEnemyKilled(object sender, EventArgs e)
    {
    AmmoRegen();
    }



    public void AmmoRegen()
    {
    if (this.gameObject.activeSelf)
    {
    gunData.currentAmmo = gunData.currentAmmo + gunData.magSize / 2;
    gunData.currentAmmo = gunData.currentAmmo + 1;
    if (gunData.currentAmmo > gunData.magSize)
    gunData.currentAmmo = gunData.magSize + 1;
    }
    }


    The GameObject deletion works fine but where I'm running into issues is getting to event to trigger on all objects with the "Target" script attached. Currently, the event will only trigger on a weapon with the "Gun" script attached if it destroys the one GameObject set as the "Target" within the "Gun" script.

    upload_2022-10-2_13-51-15.png

    upload_2022-10-2_13-49-15.png

    I've used an array to grab all objects with a certain tag so that they become "Targetable", but my question is how do I make the actual target variable use the array of "Targetable" GameObjects as the input, so that my weapon will receive the event when destroying all GameObjects in the array instead of on the one set as "Target".

    Essentially, how do I get this
    upload_2022-10-2_13-49-15.png
    to use this
    upload_2022-10-2_13-52-49.png
    as the "target" input.
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    When posting chunks of code, you should use the first option, not the second one. Makes it way easier to read.
    upload_2022-10-2_16-39-50.png
    After reading the problem, I'm still having trouble figuring out what you actually want.
    I don't really understand what this means. Are you trying to pass the Targetable array as a parameter for a method inside of Target? It might help using actual game examples instead of describing it.
     
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    I understand what you want, but that's a very roundabout question indeed.
    Do you know how lists and arrays work in general?

    What stops you from promoting
    Target target
    to
    Target[] target
    and simply passing the result as an array?
     
  4. ThickyVEVO

    ThickyVEVO

    Joined:
    May 28, 2020
    Posts:
    16
    Because
    Target target
    is a reference to my target script and when I try to make it
    Target[] target
    it makes it so the event subscription doesn't contain a definition, like this:
    upload_2022-10-3_16-33-51.png
    upload_2022-10-3_16-32-48.png upload_2022-10-3_16-33-12.png

    assuming that's what you meant. I haven't had to use arrays outside of basic integers so I'm obviously not the most experienced with them.
     

    Attached Files:

  5. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    You're supposed to loop through the array and apply your code to each Target individually. You can use a for loop or a foreach loop, those are the most common ways.