- Home /
removing instantiated game objects after time
Hey guys I'm super new to coding and want to learn. I am trying to make a simple(not so simple) 2D android game learning as I go. I am currently stuck where the instantiated gameobjects are not being removed after a certain time period. I don't have any errors or warnings so I at a loss what to do next although I suspect maybe it's got somehting to do with this code
IEnumerator removeoldweedfromlist(List list) { yield return new WaitForSeconds (4.0f); list.RemoveAll (item => item = null);
I have been trying to figure this out for hours and have realized it must be the item => item part?
Anyway, I have attached the screen shot of the entire script as well.
thank you in advance I applaud anyone who knows anything about coding.
oh ok good to know i'll keep that in $$anonymous$$d in the future
here is the code
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Createblueweed : $$anonymous$$onoBehaviour {
public GameObject initialblueweed;
public float timefornewweed;
public float currentbluetime = 0.0f;
public Vector3 initialblueweedposition;
List<GameObject> newblueweed = new List<GameObject>();
public GameObject prefab;
void Start () {
newblueweed.Add (initialblueweed);
initialblueweedposition = initialblueweed.transform.position;
}
void Update () {
createnewblueweed ();
StartCoroutine (removeoldweedfromlist (newblueweed));
}
void createnewblueweed() {
if (Time.time - currentbluetime > timefornewweed) {
currentbluetime = Time.time;
//creating a new instance of a new blue weed
newblueweed.Add(Instantiate (newblueweed[newblueweed.Count - 1], new Vector3 (initialblueweedposition.x, initialblueweedposition.y + randomYoffset(), initialblueweedposition.z), Quaternion.identity) as GameObject);
newblueweed[newblueweed.Count - 1].name = "newblueweed" + newblueweed.Count;
}
}
float randomYoffset() {
return Random.Range (-4f, 4f);
}
IEnumerator removeoldweedfromlist(List<GameObject> list) {
yield return new WaitForSeconds (4.0f);
list.RemoveAll (item => item = null);
}
}
No worries. :) Published the question now.
Just for convenience, could I get you to please edit the comment and mark the section with the code and hit the format (1010101) button a couple times until it formats correctly?
Your answer
Follow this Question
Related Questions
Instantiate an object as soon as another object is destroyed 2 Answers
Custom class with coroutine can't be added using the "new" keyword 1 Answer
How to wait scene to load if using IEnumerator Start()? 0 Answers
Endless runner obstacles spawning problem 1 Answer
IEnumerator not looping correctly? 1 Answer