Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by moonLite · Apr 11, 2012 at 11:12 PM · instantiatespawn

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image save · Apr 11, 2012 at 11:28 PM 0
Share

"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.

avatar image moonLite · Apr 12, 2012 at 04:40 PM 0
Share

@save

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best 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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image moonLite · Apr 12, 2012 at 06:12 PM 0
Share

@raoz,

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?

avatar image raoz · Apr 12, 2012 at 06:54 PM 0
Share

var spawn1 : GameObject; var spawn2 : GameObject; and drag your spawnpoints on them

avatar image moonLite · Apr 12, 2012 at 07:21 PM 0
Share

what is "int"? I can't really guess it. I'm trying it out now.

avatar image raoz · Apr 12, 2012 at 07:40 PM 0
Share

Sorry,that was wrong, I updated my script. It should be correct now

avatar image moonLite · Apr 13, 2012 at 06:39 AM 0
Share

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.

Show more comments
avatar image
0

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

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aldonaletto · Apr 12, 2012 at 02:45 AM 1
Share

NOTE: GameObject.Find only finds objects in scene, thus at least one enemy must be in scene to be found!

avatar image Kleptomaniac · Apr 12, 2012 at 06:41 AM 0
Share

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

avatar image moonLite · Apr 12, 2012 at 05:18 PM 0
Share

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.

avatar image moonLite · Apr 12, 2012 at 05:20 PM 0
Share

@$$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.

avatar image moonLite · Apr 12, 2012 at 05:25 PM 0
Share

And sorry for the long post, i tried to space & line already but it still looks quite messy & cram.

Show more comments
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Having Trouble with Instantiating an object on an axis 2 Answers

How would I create a script that spawns objects more frequently as time goes on? 3 Answers

Spawn: Instantiate GameObject 1 Answer

Error: Instantiated Enemies don't get hit 2 Answers

Issue With Spawning Enemies (javascript) 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges