I want to spawn a gameobject at x seconds after game begins. For this I want the time to be random so I use x = Random.Range(0,endOfTheDay);…
I also want this to contain information about the gameobject. Like name, age etc…
But I need the name to be printed before the spawn happens!
So it will be: at time x a character will be spawned with name “Alfred” age:25
There will be many more characters that will be spawned so how do I assign “Alfred” and 25 to the time x?
Can I use a class for the time x somehow?
After a second reading of your post, what you’ve described is a queue of work. It’s just a list (perhaps List<>) of classes that contain name, age and time in seconds. This works best if the list is sorted by time in seconds, so it is in the order you intend to create the characters.
Each cycle (Update) you check the next (initially first) entry in the least (which should be the next character to spawn at some point in the future). If that time is matched (or expired), you can print the name (at any time before spawning), spawn, then move a ‘counter’ forward to the next entry in the list, and let it move the ‘check’ at the beginning of the next paragraph wait until that time expires, continuing to the end of the list.
I sense you’re quite new, possibly a young student. I’m posting pseudo code to illustrate (it’s untested example, not checked, likely valid code with no guarantee it doesn’t need minor fixes or adaptation to your exact situation).
public class Character
{
public float TimeToSpawn;
public string Name;
public float Age;
}
....in some other class that 'owns' a list of these
List< Character > characterList = new List< Character >();
Look into the List<> documentation for how to add to the list, sort it (if you require), and get entries from it. Further, you might prefer long or integer for age and time to spawn, up to you.
I’ve once created a simple enemy wave spawning system over here. It’s just a very simply setup and each wave action simply spawns a certain amount of enemies of a certain type. The waves can be setup in the inspector. Of course if you need other kind of actions to take place you have to account for those as well. For example if you only want to proceed to the next wave when all enemies of the current wave have been killed you have to do that yourself.
Your description of your question is very abstract so we can’t get into any details if we don’t know the exact behaviour you want. From the vague description i guess you don’t even know what you actually want. So i suggest to think about your actual goals and requirements and write it down somewhere. Writing game design documents has several benefits. First of all you make yourself clear what you need. In addition it’s the first step for documenting your software.