- Home /
Question by
MrsSolarSystem · Jan 07, 2019 at 05:38 PM ·
c#listrandom
Can't remove number from list,Random number from list and delete it from list
I want to make a reward number that pop off from balloon when bullet hit.
So I create a List that contain a reward number. When bullet hit balloon it will random number from list show it and delete from list.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Bullet : MonoBehaviour
{
public GameObject RewardNumberprefab;
public List<int> list;
void Start()
{
list = new List<int> { 0, 1, 2 };
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Balloon")
{
InitializeRewardNumber();
GameObject effectIns = (GameObject)Instantiate(bulletHitBalloonEffect, transform.position, transform.rotation);
Destroy(effectIns, 2f);
Destroy(collision.gameObject);
Destroy(gameObject);
}
if (collision.gameObject.tag == "Wall")
{
GameObject effectIns = (GameObject)Instantiate(bulletHitWallEffect, transform.position, transform.rotation);
Destroy(effectIns, 1f);
Destroy(gameObject);
}
}
void InitializeRewardNumber()
{
GameObject temp = (GameObject)Instantiate(RewardNumberprefab, transform.position, Quaternion.identity);
temp.SetActive(true);
int randomIndex = Random.Range(0, list.Count);
Debug.Log(randomIndex);
int randomNumber = list[randomIndex];
Debug.Log(randomNumber);
Debug.Log("---------------------------");
temp.GetComponent<RewardNumberController>().SetText(randomNumber.ToString());
list.Remove(randomIndex);
foreach (int i in list)
{
Debug.Log(i);
}
Debug.Log("---------------------------");
Destroy(temp.gameObject, 4f);
}
}
But my problem is when I run it it remove the number in my Debug.Log
but it not deleted from the list. I don't known what I have done wrong.
Comment
Your answer
Follow this Question
Related Questions
Pick between two floats 2 Answers
Random element from the list 1 Answer
Printing a GUI selection grid in order 1 Answer
How can I display 4 items in random order but never double? 2 Answers