Repeating number of times

Is there a nifty way to call a function a certain number of times? I want to Instantiate a object a certain number of times. I tried all the doc examples and anything else i could find and they just don’t work out. What i’m trying to do is drop an item from inventory that’s stacked and have the item cloned by that amount. Another problem i’m having is when they are Instantiated they Instantiate on top of each other so the colliders shoot them a virtual mile apart.

What i’m going for is something like when you drop something in oblivion and the items kind of fall in all directions. I have messed around a little with random.Direction and a few other methods, but no real luck.

Any ideas/logic on this subject would be awesome. :smile:

internally stored counters. Pseudocode ahoy!

for(int count=0;count<4;count++)

or for a frame-by frame basis:

public int lifetime = 8;

public void live()
{
  this.lifetime--;
  if(this.lifetime==0)
  {
    this.Destroy();
  }
}

public Instance(int life)
{
  this.lifetime = life;
}

I would assume you could use a for loop. For the position, you might be able to use random.insideUnitSphere, but I don’t think that would work perfectly. I think unity 3 will have some kind of collision ignoring by layer feature, so that could work when unity 3 is released, but for now I’m not sure. Here’s something that might work for now:

for (i=0;i<=theAmountOfTimes;i++) {
     pos = transform.position+random.insideUnitSphere*someAmount;
     Instantiate(thing,pos,random.rotation);
}

Yeah, in this case, use a for loop, and a Quaternion located at the center of the dropping object. Turn the quaternion by 360/totalcount around the up axis, then add radiusforward to the vector, and your new vector is the world position of the item. This is EXACTLY how oblivion handles this behavior.

Hey thanks for the great examples. And after looking at some of your post it looks like we have two more great additions to the community. :smile:

Yeah, thanks. I’m kind of a programming veteran. I’m sick of writing my own 3D engines, and never finishing anything because I get bored with all the math “meat and potatoes” and can’t skip straight to the “dessert work”. Though, I’ve just started up with Unity this weekend, so I’m still learning the API. Luckily, I was already pretty well versed in C# (As well as nearly 30 other programming and scripting languages), so it’s all about exploring that thar API.

Back to the point:

I’d still like to point out that you are probably going to have quite a few issues with items spawning inside of each other and shooting off randomly using my example. I’d suggest checking to see if the distance between points on the sphere will allow for the unit size of items if you want to prevent this… Plus, you are going to need some way of testing collision with walls so players can’t spew items through doors, and inside of world geometry.

It’s a bit of a headache to test for every situation possible, to be honest.

I may have it so that a transform right above the player’s head just pumps out the right amount one after another. cause the item becomes null from my array then becomes a child of the options transform where through gui i decide to drop it, delete it, or put it back in the array. So i tried to instantiate it’s child like any other projectile gun to its stack number, then unparent the last item. but i couldn’t figure out how to invoke the instantiation to the int. That way i could time delay the whole thing to avoid collision. It would look like i just threw up my items lol cause i added a little velocity. But like most its sometimes better to work correctly than look good. Invoke.Repeating could use that third parameter for number of repeats. :smile: