I am very new to Unity and am interested to see if I can spawn specific game objects at specific times and in specific locations.
E.g. 10:01 Red cube at location 1
10:02 blue triangle location 4
10:03 red sphere location 2 and blue cube at location 6
and so on.
I was hoping for an excel style lookup table. Looking at the time, then the object, texture, and location.
I really haven’t got a clue where to start. Any help would be appreciated.
Time.time will give you the current time since the start of the application, if you intend to gauge the time since the start of a given level you’ll need to log the Time.time at start of the level and work out the difference.
Spawning is done through “Instantiate”… lots of tutorials on that.
You might find a look at scriptable objects useful as a storage medium for holding the data or at the very least “non monobehaviour” classes and arrays to store the information somewhere (looking up “unity spawn waves” on google should also point you towards useful media on various approaches).
You can use a TextAsset, load it into a variable and read it line by line.
You would need a function to parse one line of the text to get the necessary information, but how you encode this is up to you. The simplest way I can think of would just be integers, which then are simply used as index in an array of ‘Encounters’ and positions.
One example (untested and probably full of errors…and missing safety-mechanisms)
using System.Linq;
struct Encounter {
int encounterIndex;
int positionIndex;
int number;
}
TextAsset textAsset;
string[] encounters;
int encounterIndex = 0;
void Init() {
encounters = textAsset.text.Split(new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
}
Encounter GetNextEncounter () {
string encounter = encounters[encounterIndex];
int[] encounterData = encounters.Split(new string[] {";"}).Select(s => System.Convert.ToInt32(s)).ToArray();
encounterIndex++;
return new Encounter {
encounterIndex = encounterData[0];
positionIndex = encounterData[1];
number = encounterData[2];
}
}