3

Hi I am making my first unity game in 2D and I have this code now. I have 3 different GameObjects (call them a, b , c) in the array arrows and i would want to know which one of them is spawned ( so i can use it in another function) and delete the previous one from the scene. Now it just spawns one GameObject on another every 5 seconds and I don't know which one of them did it randomly spawn. Any idea?

public GameObject[] arrows;
public float interval = 5;

// Use this for initialization
void Start()
{
    StartCoroutine(SpawnArrow());
}
public IEnumerator SpawnArrow()
{
    WaitForSeconds delay = new WaitForSeconds(interval);
    while (true)
    {
        GameObject prefab = arrows[UnityEngine.Random.Range(0, arrows.Length)];
        GameObject clone = Instantiate(prefab, new Vector3(0.02F, 2.18F, -1), Quaternion.identity);
        yield return delay;


    }
}`
2
  • You just use the object you created as a parameter. you put a YourOtherFunction(clone)in the while loop. If that is not what you are looking for you need to provide more deatail about what you are trying to do. Include the other function too in your code example. Commented Apr 11, 2017 at 16:41
  • Also, if you are asking about Unity you need to put the unity3d tag on your question. Otherwise you may get answers that are not appropriate to use on Unity. Commented Apr 11, 2017 at 18:45

1 Answer 1

3

To keep track of what objects you instantiated you could create a List of GameObject and then when you instantiate an object you can add it in the List. Then you can use any GameObject in that list to do whatever you need.

public GameObject[] arrows;
public float interval = 5;
private List<GameObject> myObjects = new List<GameObject>();

// Use this for initialization
void Start()
{
    StartCoroutine(SpawnArrow());
}
public IEnumerator SpawnArrow()
{
    WaitForSeconds delay = new WaitForSeconds(interval);
    while (true)
    {
        GameObject prefab = arrows[UnityEngine.Random.Range(0, arrows.Length)];
        GameObject clone = Instantiate(prefab, new Vector3(0.02F, 2.18F, -1), Quaternion.identity);

        //Add the object to the list
        myObjects.Add(clone);

        yield return delay;


    }
}

And for spawning object like that it would be better to use InvokeRepeating, you can pass it the repeat rate and when you want it to start. More information on InvokeRepeating

This is how it could look like with InvokeRepeating

public GameObject[] arrows;
public float interval = 5;
private List<GameObject> myObjects = new List<GameObject>();

// Use this for initialization
void Start()
{
    InvokeRepeating("SpawnArrow", 0f, interval);
}

void SpawnArrow()
{
    //Check if there's something in the list
    if (myObjects.Count > 0)
    {
        //Destroy the last object in the list.
        Destroy(myObjects.Last());

        //Remove it from the list.
        myObjects.RemoveAt(myObjects.Count - 1);
    }

    GameObject prefab = arrows[UnityEngine.Random.Range(0, arrows.Length)];
    GameObject clone = Instantiate(prefab, new Vector3(0.02F, 2.18F, -1), Quaternion.identity);

    //Add the object to the list
    myObjects.Add(clone);

}

You can destroy the last spawned object in the list by doing Destroy(myObjects.Last()); it will return the last object in your list and destroy it. You should then also remove it from the list myObjects.RemoveAt(myObjects.Count - 1);

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks. How to I know which object spawned last (i would like to use it like: if object a spawned then... ) and how do i know which object spawned before it so i can destroy it from the scene? (i am using this InvokeRepeating code now) :D
@KlemenŠkrlj I added at the end of my answer how you could do it.
And where in the code do i put this two lines? (I know.. I am a noob) It said that i have to add System.Linq and when i run it i says:InvalidOperationException: Operation is not valid due to the current state of the object System.Linq.Enumerable.Last[GameObject] (IEnumerable`1 source)
@KlemenŠkrlj I edited my answer could you try it like that and you need to add using System.Linq; at the top of your script.
Just a final question since you know everything: How do i check which object did it spawned? How do i implement this into an if statement?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.