- Home /
Question by
jeffmccown4 · Oct 23, 2021 at 06:02 PM ·
colliderlist
How to detect and remove item in spawned gameobject list when destroyed. Basically letters of alphabet randomly spawned and want to remove so they don't repeat same letter.
This is my spawner code.
public class SpawnList : MonoBehaviour { // Start is called before the first frame update public List letterList; private float spawnRangeX = 27; private float startDelay = 2; public float spawnInterval = .05f;
void Start()
{
InvokeRepeating("RandomLetter", startDelay, spawnInterval);
}
void RandomLetter()
{
Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), 6, 0);
int randomLetter = Random.Range(0, letterList.Count - 1);
letterList = new List<GameObject>(Resources.LoadAll<GameObject>("Letters"));
Instantiate(letterList[randomLetter], new Vector3(Random.Range(-spawnRangeX, spawnRangeX), 6, 0), Quaternion.identity);
}
}
this is my Letters code public class Letters : MonoBehaviour {
[HideInInspector]
public float speedLetter;
private Rigidbody2D myBody;
// Start is called before the first frame update
void Awake()
{
myBody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate()
{
myBody.velocity = new Vector2(myBody.velocity.x, -speedLetter);
}
public void OnTriggerEnter2D(Collider2D collision)
{
Destroy(gameObject);
}
}// class
Comment
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How To Get A Reference To All Nearby GameObjects? 1 Answer
Internal collisions 1 Answer
How do i access a SphereCollider added by RequireComponent? 1 Answer