- Home /
How to fix my enemy Instantiate script? its clone too many
Hi Guys,
I'm trying to make my enemy come out from either left & right screen. And I attached this script to my enemy.
var speed:int = 1;
var enemyslot:GameObject;
function Update () {
transform.position.x -= speed * Time.deltaTime;
if(transform.position.x > 6.1 || transform.position.x < -6.1)
{
Instantiate(enemyslot: transform.position, Quaternion.identity);
transform.position.x = -7 || 7 ;
transform.position.y = Random.Range(-2.3, 1.4);
transform.position.z = -3;
}
}
1a)) when it tested play, the enemy move out of the screen, it created too many till unity crashed. So how do i add spawn, limiter or time to fix that?
1b) any other advices? like should i put it inside another script or something?
2) In this game object (inside a prefab), i created a enemyslot gameobject, then i drag its own prefab to it, so for my Instantiate.
How do I work around without having to create a gameobject slot & put itself under its own gameobject slot to Instantiate?
Thanks in advance!
"How do I work around without having to create a gameobject slot & put itself under its own gameobject slot to Instantiate?"
Could you elaborate this part, you will always need to assign a GameObject somewhere to instantiate it. To do it strictly via code you'd use a resource folder.
I was thinking if i can do without dragging my prefab enemy slot to game object slot i created to instantiate. my "enemy" is prefab.
function Start ()
{
EnemySlot = Resources.LoadAssetAtPath("Assets/Prefabs/enemy1", GameObject);
}
I tried this way, and it works! since documentation rest is all load from the Resources folder Only which I don't want.
I have a question, i noticed from the documentation, http://unity3d.com/support/documentation/ScriptReference/Resources.LoadAssetAtPath.html
It said "Assets/Artwork/mymodel.fbx"
but if my is a prefab, there's no need for me to add extension of prefab behind the name, Unity will auto detect, right?
Thanks again save for the answer
Answer by raoz · Apr 12, 2012 at 05:36 PM
function spawn()
{
var time : int = Time.realtimeSinceStartup * 1000;
if((time & 0x01) == 0) //Checking if the time millisecond count is an even number(To make it come out of random side)
{
Instantiate(enemyslot, spawn1.transform.position, Quaternion.identity);
}
else
{
Instantiate(enemyslot, spawn2.transform.position, Quaternion.identity);
}
}
When you call this function it will spawn 1 enemy in one of the spawns. It needs 3 variables - enemyslot -- this will be the enemy, spawn1 and spawn2 -- these are the spawns.
Thanks raoz,
var enemyslot: GameObject; // drag my enemy prefab into it. same as old code, if it isn't the same enemyslot as my original, please let me know.
1) Regarding Spawn1 and Spawn2, what should i declare as?
var spawn1 : GameObject; var spawn2 : GameObject; and drag your spawnpoints on them
what is "int"? I can't really guess it. I'm trying it out now.
Sorry,that was wrong, I updated my script. It should be correct now
Thanks! it works thought it still clone too many from left & right. And its seems facing flat down ins$$anonymous$$d of facing towards the camera. I think it has to do with the rotation, Quaternion.identity.
Answer by Kleptomaniac · Apr 11, 2012 at 11:29 PM
To add a spawn limiter, just set up a count with a for loop, and for question 2, I believe you want a GameObject.Find?
var speed: int = 1;
var enemySlot : GameObject;
var spawnLimit : int;
private var canPos : boolean = true;
function Update () {
transform.position.x -= speed * Time.deltaTime;
if(transform.position.x > 6.1 || transform.position.x < -6.1) {
for (var count = 0; count < spawnLimit; count++) {
if (count <= spawnLimit) {
var enemyInst : GameObject = Instantiate(enemySlot, transform.position, Quaternion.identity) as GameObject;
canPos = true;
if (canPos) { //I am just setting up a boolean flag so that the position doesn't jump everywhere with Random.Ranges
enemyInst.transform.position = Vector3(Random.Range(-7, 7), Random.Range(-2.3, 1.4), -3);
canPos = false;
}
}
}
}
}
I'm pretty sure that should work.
Hope that helps, Klep
NOTE: GameObject.Find only finds objects in scene, thus at least one enemy must be in scene to be found!
Oh, yes that's true. O$$anonymous$$, @moonlite, looks like you'll just have to assign the prefab to "enemySlot" normally through the inspector.
Thanks @aldonaletto :P
thanks @aldonaletto & @$$anonymous$$leptomaniac!
I got the solutions from save. But I guess i will avoid that, it's will add up more coding, I want to avoid as much coding as possible. I will stick with creating var gameobject and drag my prefabs.
@$$anonymous$$leptomaniac ,
I tried your code, it seems that after awhile, then it will Instantiate the enemy slot, GameObject. And it clone TOO $$anonymous$$ANY just like old time. but ins$$anonymous$$d creating gameobjects flat, the 2d texturing facing me, it created like a down road direction texture, head of the enemy facing me.
I created a game object, called Spawnpoint, and attached the code to it.
So I have a few questions,
1) what is the CanPos actually about? I don't really understand setting the boolean flag so that that the position doesn't jump everywhere with Random.Ranges. I tried to comment on it, it seems it doesn't matter. What is it actually about?
2a) from the coding, I more want the x-axis be like a || or statement, rather than the random range. I want the enemy to come out from the world axis, from -7 or 7 randomly.
So I tried
enemyInst.transform.position = Vector3((7|| -7), Random.Range(-2.3, 1.4), -3);
It didn't randomly come out from left or right of the screen. Should I be using Vector or transform position or even translate? It seems Vector((7||-7)
doesn't work. How do I make it come out from left or right?
2b) but I noticed it's because of this transform.position.x -= speed * Time.deltaTime;
So i tried to comment it out, but because of that spawn point not moving but waited for soemtimes, the enemy prefab doesn't create by itself. Is it because of the Time.deltatime? How do I solve without making the spawn point moving yet keep the enemy prefabs to create by itself?
3) Why this code, it will wait like for around 6-7sec then it will start Instantiate? which part of the code make the unity wait for 7sec then Instantiate the enemy gameobject? How do i set the timer and when to set it?
Like 1st sec, it will created a 5 randomly to enter into the screen, then pause for awhile etc.
Thanks guys. Please let me know if I didn't explain it clearly.
Just a side now, previously i was attached the above code I 1st posted on my enemy prefab. Then i realized, i have to create a spawn point to generate my enemy prefabs. So I doing it in this case now.
What i want to create is my enemies come out from left and right (the Y axis is random.range) outside of the screen. And enter into the game screen.
And sorry for the long post, i tried to space & line already but it still looks quite messy & cram.
Answer by aldonaletto · Apr 13, 2012 at 11:34 AM
I answered a question about enemy spawning some time ago, where the enemies were being created by dozen without any limit. Maybe the answer can help you - take a look at http://answers.unity3d.com/questions/169533/spawing-enemies-works-but-works-to-good.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613070214im_/https://answers.unity.com/themes/thub/images/avi.jpg)