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. Dismiss Notice

How to check that a random object from the array has been destroyed?

Discussion in 'Scripting' started by kmhworking, Jan 20, 2021.

  1. kmhworking

    kmhworking

    Joined:
    Jan 20, 2021
    Posts:
    6
    There is an Array of game objects (random bonuses in the game). Each object has a trigger that is triggered if a player passes through it, after triggering the object is destroyed. How to check that a random object from the array has been destroyed? As planned, it should be created again after destruction.

    1. public Transform spawnPlace;
    2. public GameObject[] spawnBonuses;
    3. public float timeSpawn;
    4. private int randomBonus;
    5. void Start()
    6. {
    7. Invoke("CreateBonus", timeSpawn);
    8. }
    9. private void Update()
    10. {
    11. if (spawnBonuses == null)
    12. {
    13. Invoke("CreateBonus",2f);
    14. }
    15. }
    16. public void CreateBonus()
    17. {
    18. randomBonus = Random.Range(0, spawnBonuses.Length);
    19. GameObject myBonuses = Instantiate(spawnBonuses[randomBonus], spawnPlace.transform.position, Quaternion.identity);
    20. myBonuses.transform.parent = transform;
    21. Debug.Log(spawnBonuses);
    22. }

    I am writing here because I have been waiting for moderation on answers.unity for 7 hours already, I found a similar message and there they advised me to write to the forum, I looked for arrays but did not find it, I apologize if not on the topic.
     
  2. CatchFires

    CatchFires

    Joined:
    Jul 19, 2020
    Posts:
    38
    Theres probably a few ways to go about it, guess it depends on how you want them to spawn, do you want the exact same bonus to spawn? Do you want it to spawn as soon as the other one is destroyed? if thats the case you could just have a spawn method that takes an int, give your spawnbonuses an int field and when you spawn it set that to it's position in the array and then just before you call the destroy function, you use the spawn method with the bonuses value.

    I mean, theres gonna be 1000 ways to do it
     
  3. kmhworking

    kmhworking

    Joined:
    Jan 20, 2021
    Posts:
    6
    I'm new to c #, I would like to create bonuses (after destruction), after a certain time, I thought I would do it by checking the array, but I don’t know how to check it ... maybe I’m thinking in the wrong direction ... As a result, I want to get the next ones, the player takes the bonus, the bonus disappears, and appears in the same place after a while. I thought to implement it using Invoke, but I'm a beginner and I can't be sure that this is the right solution ... if you can tell me how you would do it.
     
  4. CatchFires

    CatchFires

    Joined:
    Jul 19, 2020
    Posts:
    38
    Well, there are lots of ways to do it, I would probably do something with event listeners but if you're only a beginner that might be a bit out of your understanding.

    Have you played with Coroutines before?

    In basic terms it's a method that can pause and wait for instruction.

    Since your bonus objects are going to stay in the same place, rather than destroy them you can set them inactive, right before you do, you can start a coroutine, use a yield WaitForSeconds and then reactivate the object.

    That's one way.
     
  5. kmhworking

    kmhworking

    Joined:
    Jan 20, 2021
    Posts:
    6
    The fact is that if I use a coroutine, I won't get what I want ... There is an array of bonuses, it contains prefabs with bonuses that are randomly created, the player takes the bonus and the bonus disappears, I need it to appear again (there is in view of the array with bonuses in the same place), but after a while, and if the player did not pick up the bonus, then nothing happened, all I need is as in C # code, whether the array was taken (or it was destroyed, or not) how to check it, if you know please write with the code. Right now my check looks like this (private void Update () { if (spawnBonuses == null) { Invoke ("CreateBonus", 2f); } }) but it doesn't check ... = (I'm looking for a similar one on the Internet and can't find a similar problem.
     
  6. kmhworking

    kmhworking

    Joined:
    Jan 20, 2021
    Posts:
    6
    I solved the problem with the help of a bool variable, in the instances of bonuses it takes a false. Faced a bug, if you have one prefab (with an array of bonuses), and there are several prefab instances on the stage, then they may not work correctly, for example, in one, nothing will be created, and in the other, several bonuses at once. The solution is to create several different prefabs.

    1. public Transform spawnPlace;
    2. public GameObject[] spawnBonuses;
    3. public float timeSpawn;
    4. private int randomBonus;
    5. public static bool bonusStand;
    6. void Start()
    7. {
    8. Invoke("CreateBonus", timeSpawn);
    9. bonusStand = true;
    10. }
    11. private void Update()
    12. {
    13. if (bonusStand == false)
    14. {
    15. bonusStand = true;
    16. Invoke("CreateBonus",10f);
    17. }
    18. }
    19. public void CreateBonus()
    20. {
    21. randomBonus = Random.Range(0, spawnBonuses.Length);
    22. GameObject myBonuses = Instantiate(spawnBonuses[randomBonus], spawnPlace.transform.position, Quaternion.identity);
    23. myBonuses.transform.parent = transform;
    24. bonusStand = true;
    25. Debug.Log(bonusStand + " createbonus");
    26. }
    Thank you for giving the direction of thought =)
     
  7. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    Instead of the bool, just have the collected bonus object call the Invoke method from its script when it's collected. It could also pass information about itself if needed, like which one it was.