Hi,
I have a Void which Instantiate a random List, then it auto Invokes in random interval.
Does anyone know how I could go about it, so that if my item number 3 in my index is instantiate, I can modify the value of minRandomInterval of my Invoke.
I need to balance my games.
To summarize, I have a spawn of a list of items, all the Randoms intervals, it invokes my void which instantiates a random object from my list.
And to balance my game, I need, that if item 3 spawns, the minimum interval before a new spawn is set to a chosen value.
Would be helpful if you post the code in question.
By “then it auto Invokes in random interval”, do you mean you fire off a coroutine? Just give the coroutine a parameter which is uses to determine the time between the next spawn.
spiney199 I didn’t understand what you wanted to do with the coroutine??
Something like this??
void Spawn()
{
(....)
StartCoroutine(myCoroutine());
}
IEnumerator myCoroutine()
{
yield return new WaitForSeconds(xf);
Invoke("Spawn", Random.Range(0, 0)); //I know I don't have to put random.range, just 0 its ok^^
}
Euuuuuuuu … uuuuu … uuuu ok ^^ I don’t have the level yet to understand it well, I’m fed up when I start to make progress, I get codes that I don’t understand :'(, but thank you, I’m sure it will be very useful to me later, but I’m learning little by little huh
The principle is pretty simple, you make an object that holds all the information you need and use that in a collection rather than just game objects. In this case its a serializable plain class, which you can view and edit in the inspector.
Then at run time, you can pull a random one out, and you have both the object to spawn and the interval to the next spawn.
Encapsulation like this is a pretty important concept you’ll need to understand.
Again I liked this way of writing it : [System.Serializable], I guess it’s a way of saying to serialize everything that contains the class ItemSpawnWrapper.
{ get; private set; } never seen either, but I would say, setting up a getter setter, or set and private get in public.
public List<ItemSpawnWrapper> spawnList = new List<ItemSpawnWrapper(); I would say an obscure method, to retrieve, transform or name the list relative to the variables.
Quaternion.Identity, I know with vectors but not with identity, I have to find out.
The coroutine I was not too developed but I looked, and with the example it is easy to understand.
Although I can’t figure it out in C# yet, I guess what you call Encapsulation, and some sort of concatenation.
It’s an attribute in the System namespace. While not a Unity attribute, it tells Unity it should attempt to serialise a non-Unity object. Meaning it attempts to write it to disk so that you can edit it in the inspector and have changes persist.
I suppose those were a bit advanced. It’s a property - not sure if you’re familiar with those - and an auto implemented one at that. It’s shorthand code where this:
public int SomeInt { get; set; } = 10;
It’s the same as this, except it’s done automatically under the hood:
private int someInt = 10;
public int SomeInt { get { return someInt; } set { someInt = value; } }
The [field: SomeAttribute] is syntax for applying the attribute to the invisible backing field of the auto property, rather than the property itself. It’s the same as putting an attribute like [SerializeField] on a regular field (remember, properties aren’t field, they’re methods).
The { get; private set; } is for the purposes of immutability (making outside sources unable to change it). Sometimes you want this, sometimes you don’t. It’s often good practice to only give as much access as you need to.
This is where the encapsulation occurs. The = new List<ItemSpawnWrapper(); is just there to initialise the list so it isn’t null. It’s actually not needed when working with serialised lists, but it’s a good habit to have.
Quaternion.Indentity is just no rotation; it was just for example purposes. You can have whatever rotation you want in there.
I guess? It’s more putting like putting everything you need into a bag, so that when you get everything you need out of it.