your heart by keeping your cholesterol levels in check. Why Does Glycemic Index Vary Among Foods? detoxifying and fat burning properties. The secret to this lies in the high concentrations of
nation, involve the transfer of information It is also a desirable feature of compe- mosomes observed in interphase nuclei
maintenance plan you’ve ever tried? 10. a. Whole-grain foods are loaded with filling fiber. genetic and drugs. Hoping that you can achieve what they achieve or, even worse, trying to use
stored in the bones and teeth. About 1 percent of calcium circulates Seated Row Push-up Superman or Superwoman branes. Certain carbohydrates are also key portions of indispensable
Looks like your best bet is to define a static array and populate it via the inspector:
var balls : GameObject[];
function Start() {
Spawn();
}
function Spawn() {
for (i=0;i<5;i++) {
yield WaitForSeconds(1);
Instantiate(balls[Random.Range(0,3)], transform.position, transform.rotation);
}
}
If you are going to spawn 1 of 3 types ever 3 seconds for an unknown amount of time, you might want to look into Clone.
Hi Matt,
Can you guide me on how to use the static array? I don’t completely understand what you mean, sorry.
Hi Zumwait,
You mentioned clone, do you mean the instantiate component? Or it is something entirely different?
Thanks!
Just put the items in the array in the inspector. Although the code would be improved a bit by doing:
Instantiate(balls[Random.Range(0, balls.Length)], transform.position, transform.rotation);
That way you can make the array any size and the random function will always work.
–Eric
Thanks Eric!
I can see now that the array.length makes life easier when I expand my array.
Originally I couldn’t insert the items in the array in the inspector because of a compilation error, setting the array as static as well as ball1-3 gameobjects solves the compilation error, but apparently setting up static on the gameobjects does not allow me to insert the items also.
static var ball1: GameObject;
static var ball2: GameObject;
static var ball3: GameObject;
static var BallArray = new Array (ball1, ball2, ball3);
function Start() {
Spawn();
}
function Spawn() {
for (i=0;i<5;i++) {
yield WaitForSeconds(1);
Instantiate(BallArray[Random.BallArray.length], transform.position, transform.rotation);
}
}
I ended up placing the array in the spawn() function instead and it works.
var ball1: GameObject;
var ball2: GameObject;
var ball3: GameObject;
function Start() {
Spawn();
}
function Spawn() {
var BallArray = new Array (ball1, ball2, ball3);
for (i=0;i<5;i++) {
yield WaitForSeconds(1);
Instantiate(BallArray[Random.Range(0,BallArray.length)], transform.position, transform.rotation);
}
}
I have a question. Why can’t I insert gameobject prefabs if when declare them as static?
I’d highly recommend using Matthew’s script instead (along with my change
). He may have misspoke when saying “static”, as you wouldn’t want to use a static array in this case.
–Eric
This is a snippet from my space invaders game, I can pass anything into the tmpType that is a GameObject, then place it accordingly. This sets up 5 rows of 10 invaders based on 3 different types, this code is called on game startup, then again on every level.
I have other code that does cleanup.
I’ve actually done some slight mods to this just to stress test my MacBook pro, and had an ungodly amount of invaders, personal survival rate on my levels was slim at that point.
void setupAllInvaders()
{
for(int iY=0;iY<5;iY++)
{
for (int i=0;i<10;i++)
{
tmpType=invList[iY];
clone=(GameObject) Instantiate(tmpType);
modScale=clone.transform.lossyScale;
pos[0]=(i*15)-modScale[0]-10;
pos[1]=modScale[1]-(20+(iY*10));
pos[2]=0;
clone.transform.position=pos;
clone.transform.parent = this.transform;
tmpObjs.Add(clone);
}
}
currentLevel +=1;
Level=GameObject.Find("txtLevel");
Level.SendMessage("UpdateLevel",currentLevel);
objInvaders=GameObject.FindGameObjectsWithTag("tagInvader");
}
ohhh i forgot to mention, Unity is a slightly different animal with regards to controlling objects in a given scene, here is a snippet that is one of my cleanups for the invaders
public void RemoveInvader(GameObject detract)
{
int i=0;
foreach (GameObject obj in tmpObjs)
{
if(obj.GetInstanceID()==detract.GetInstanceID())
break;
i++;
}
try
{
tmpObjs.RemoveAt(i);
}
catch
{
// Note, it is possible to have more than one bullet collide
// before the frame has ended, which throws a null item to the
// mix.. so just ignore the message
}
}
You see, a full game cycle goes through all known objects in the scene and executes its code, it is entirely possible to destroy an object that is in your global collection therefor it no longer exists physically but your collection still knows about it. A double destroy call can easily happen before the full game frame cycle ends. I found this about 6 months ago or so. There are smoother ways of handling this exception. Its on my other machine though.
thx guys.
Zum thx for sharing your coding and experience. Now all I need to do is to read up about C# syntax to dissect your code, I’m only familiar with Java atm. XD.
NP, about the only thing I really suggest you watch out for is cleaning up your array. For instance, without a check for errors, I had issues where the bunker was hit by 2 rockets at the same exact game frame, the bunker was already down to 1 life, this actually subtracted 2 lives in that frame and called destroy on the bunker twice. My entire invaders game is procedural, only static objects in the entire game is the box that is around the playing field which determines the ‘out of bounds’ for the bullets, and helps to keep the invaders from leaving the screen.
This worked rather amusing for my rabbit simulator. I have 1 rabit model with animation sequences setup, I then start the rabbit in a field with another rabbit, then using time calculations and some simple rabbit population math, I see how quickly they can multiply and eat all the grass.
I had added a wolf into the mix later once the population reached 100 with logic to eat the rabbits, and them with logic to run away. Sadly, I had something wrong in my devour routine for the rabbits and they were not just hurbavors. Needless to say it was an interesting massacre for the wolf. I had accidently attached the same code to not only the wolf but the rabbits for seak and destroy. Not pretty.
Does this work with iphone unity?
I ask because in iPhone unity i get a ‘Range’ is not a member of ‘Random’ error.
Your script is called “Random” also, so Unity can’t tell what you mean; nothing to do with Unity iPhone.
–Eric
Yes I knew that ** ^^
![]()
Thanks Eric ![]()