#How do I make another gameObject to spawn in its place after being destroyed using a list?

1 messages · Page 1 of 1 (latest)

remote marsh
#
public class crateMechanic : MonoBehaviour
{
    [SerializeField] List<GameObject> randomPowerUpPrefab;
    private void OnCollisionEnter(Collision collision)
    {
        Destroy(gameObject);
    }

    private void OnDestroy()
    {
        Instantiate(Random.Range(0, randomPowerUpPrefab.Count), transform.position, Quaternion.identity);
    }
}``` my current script and the problem i foresee is idk how to add power-ups on an instantiated object
solemn fiber
# remote marsh ```cs public class crateMechanic : MonoBehaviour { [SerializeField] List<Gam...
public class crateMechanic : MonoBehaviour
{
    [SerializeField] List<GameObject> randomPowerUpPrefab;
    private void OnCollisionEnter(Collision collision)
    {
        Destroy(gameObject);
    }

    private void OnDestroy()
    {
        GameObject chosenPrefab = randomPowerUpPrefab[Random.Range(0, randomPowerUpPrefab.Count)];
        Instantiate(chosenPrefab, transform.position, Quaternion.identity);
    }
}

The above should instance a random prefab from your list of prefabs. As for your other question, we can't really answer that without you sharing more context so we can understand your game.

#

For guidance please see

#

[]poorquestion