Search Unity

  1. We would like to hear your feedback about Unity and our products. Click here for more information.
    Dismiss Notice

a Newbie question

Discussion in 'Getting Started' started by truthseeker089, Jun 11, 2019.

  1. truthseeker089

    truthseeker089

    Joined:
    May 6, 2019
    Posts:
    35
    Hi guys, Im new here. I would just like to ask how to destroy a game object by name if you reached a certain number in spawned? Tags won't do for my gameplay. I want to keep on SPAWNING but I want to limit the number of objects SPAWNED, I am also okay with the time so I don't want to include it in my new script. thanks!
     
    Last edited: Jun 11, 2019
  2. truthseeker089

    truthseeker089

    Joined:
    May 6, 2019
    Posts:
    35
    like if they exceed 20 I want to destroy the objects that will still be spawned after 20 numbers because I want the spawning to continue only that I want to limit the obects that are SPAWNED to 20....
     
  3. ArtsyAngelee

    ArtsyAngelee

    Joined:
    Jun 11, 2019
    Posts:
    7
    So after you reach 20 objects you want to spawn an object and then destroy it immediately?

    Can you just stop spawning if you have 20 objects and start spawning again if you drop below 20? Or is there some reason you must keep spawning continuously even if the objects you spawn are getting destroyed immediately?

    I'd keep a list of gameobjects. Every time an object is created, add it to the list. If the list reaches 20, stop spawning, if one of the objects on the list is removed / destroyed (must add this to the code in your gameobject, ex: OnDestroy) start spawning again.
     
    JoeStrout likes this.
  4. truthseeker089

    truthseeker089

    Joined:
    May 6, 2019
    Posts:
    35
    thanks angela yes I want to keep spawning just for some effects then destroy it after a few seconds is it possible? sorry Ive been doing this for only a few weeks so you cant expect me to really know what Im doing lol
     
  5. truthseeker089

    truthseeker089

    Joined:
    May 6, 2019
    Posts:
    35
    so I'll have one object spawning until it reaches 20 quantities then the next spawned objects would be destroyed after 3sec to create visual effects sort of
     
  6. ArtsyAngelee

    ArtsyAngelee

    Joined:
    Jun 11, 2019
    Posts:
    7
    Yes, if you have a list of GameObjects, you can use ListName.Count to get the number of objects in the list.

    Ex:

    List<GameObject> ObjectList = new List<GameObject>();

    // Later in script

    GameObject Object = Instantiate(ObjectPrefab);
    ObjectList.Add(Object);

    if (ObjectList.Count > 20) {
    Destroy(Object, 3f);
    }
     
    Last edited: Jun 12, 2019
  7. truthseeker089

    truthseeker089

    Joined:
    May 6, 2019
    Posts:
    35
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class limit : MonoBehaviour {

    public GameObject Fish;
    List<GameObject> ObjectList =new List<GameObject>();


    void Start () {
    GameObject Object = Instantiate (Fish);
    ObjectList.Add (Fish);

    if (ObjectList.Count >= 20) {
    Destroy (Object);
    }


    }
    }

    I tried but it keeps on repawning hundreds in seconds I have to quickly close Unity lol sorry angel
     
  8. ArtsyAngelee

    ArtsyAngelee

    Joined:
    Jun 11, 2019
    Posts:
    7
    It's okay!

    The Start method only runs once and if you put it in the Update method, it will run once every frame.

    Wait, you're not putting this script on the Fish game object are you? I was thinking that you'd put it on an empty game object - like a Fish Holder or Fish Manager that you can parent all of the Fish objects to for organization purposes.

    That's why it's running multiple times when Start should only run once:
    A fish object runs start and creates another fish object. That fish object runs start and creates another one, and on and on.

    Since the List you have is not static (i.e. not shared between all fish objects, each fish object has it's own list with only one fish object on it (the one they created at start). So, that loop will go on forever.

    Okay, so all of that might be a little complicated for a beginner so let me break down what you should do to get it to work:
    1. Create an empty game object and name it something like Fish Holder or Fish Manager.
    2. Put this script on that game object

    (How long of a delay do you want in between spawning Fish? Since you want them to be destroyed after 3 seconds, can I assume that you don't want another fish to be spawned in before it's destroyed?)

    3. Create a coroutine (look up how to make those). This will allow you to have a delay between the spawning of fish objects (Using Destroy(Fish, 3f) by itself will destroy the created fish after 3 seconds but it will not stop more fish from being spawned in that time (which would get crazy with the effects if done every frame in Update).
    4. Put that script into the coroutine. (Create the list in the start method though, you don't want to create a new list in the coroutine or you'll have another infinite spawn problem). Put a delay of 3 seconds before destroying and creating a new fish. (Parent all new fish objects to the Fish Holder for organization).

    Let me know if you need more explanation on any of these steps.

    Edit: I've created a small example. I hope it helps you out:
    https://drive.google.com/open?id=1LV0FyNbVGP_cDbaMnlcI51w6pHVlktkK

    Any fishes over the 20 limit will be destroyed automatically. You can manually destroy fish by clicking on them. (I made the delay for fish spawning only one second so that the demo is faster but you can change the delay to 3 seconds).
     
    Last edited: Jun 13, 2019
  9. truthseeker089

    truthseeker089

    Joined:
    May 6, 2019
    Posts:
    35
    thanks angel for taking the time and trouble to even create a complete scene! truth is I've been spending the whole day figuring out how to incorporate your script into my project. I still haven't completely grasped the concept of chil/parent/list so and i'm wondering if there is any way I could use tags instead? but it seems tags.names will destroy all of the same tags/names.
     
  10. ArtsyAngelee

    ArtsyAngelee

    Joined:
    Jun 11, 2019
    Posts:
    7
    One of the reasons why programming is so fun is that there's a multitude of different ways to accomplish the same thing!

    However, with tags, how are you going to know which one is the last one that was created? (I assume you want to destroy the last one that was created, not any random fish. You could capture the time since it was created or number them but there are simpler solutions without the need for tag searches (though, if you want to use this solution, go for it!).

    I updated my example with a second scene with a solution that's simpler than using a list or tags:
    https://drive.google.com/open?id=1HJcx9wgkne7ttzNWwSBsYnyE2QuDHfIS

    The only reason I recommended using a list is so that you can easily keep track of and iterate over all of the Fish objects in the scene without having to do costly searches. The fish objects are parented to the new FIshSpawner for organization but you can remove that from the code easily and just have the fishes spawn at the root of the scene, no problem.

    Also, just to make it clear: the fish positions object / script is just for placing the spawned fish around the scene (so they're not just spawned on top of each other). This is something that you'd want to replace in your project with wherever you want them to spawn.
     
  11. truthseeker089

    truthseeker089

    Joined:
    May 6, 2019
    Posts:
    35
    1. public enum CoinFlip { Heads, Tails };

    2. //Returns either Heads or Tails
    3. public CoinFlip FlipACoin()
    4. {
    5. CoinFlip coin = CoinFlip.Tails;
    6. if (Random.Range(0, 2) == 0)
    7. {
    8. coin = CoinFlip.Heads;
    9. }
    10. return coin;
    11. }

    12. public void DestroyAllWhoGetTails()
    13. {
    14. GameObject[] whateverArray = GameObject.FindGameObjectsWithTag("Whatever");
    15. foreach (GameObject whateverObject in whateverArray)
    16. {
    17. if (FlipACoin() == CoinFlip.Tails)
    18. {
    19. Destroy(whateverObject);
    20. }
    21. }
    22. }

    yes the last one created by the way somebody also gave me some codes in my other thread, https://forum.unity.com/threads/i-am-not-sure-anyone-can-answer-this-please.694417/#post-4645072 you guys are great I will still have to check it later after school thanks angel!
     
    ArtsyAngelee likes this.
  12. ArtsyAngelee

    ArtsyAngelee

    Joined:
    Jun 11, 2019
    Posts:
    7
    Glad I could help and you're welcome! :)