Answer the question
In order to leave comments, you need to log in
How to make different assets fall randomly?
We make a 2d game on Unity3d. The language is C#. The question is this: there are falling objects in the game, from which the main character must dodge, for a change we want to make sure that not the same object falls, but different objects fall randomly. Please tell me how can we do this?
Answer the question
In order to leave comments, you need to log in
Create a list of objects, choose a random one using Random.Range :
using System.Collections.Generic;
using UnityEngine;
public class Example : MonoBehaviour
{
public List<GameObject> prefabs = new List<GameObject>();
private void Update()
{
Spawn();
}
private void Spawn()
{
var prefab = prefabs[Random.Range(0, prefabs.Count)];
Instantiate(prefab);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question