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 /
avatar image
0
Question by byurocks23 · Feb 28, 2014 at 11:31 PM · javascriptinstantiatespawnnoob

Issue With Spawning Enemies (javascript)

This is my first project on Unity so I am learning as I go. I am creating a 2d scroller game and I'm at the point where I need to spawn the enemys. I have three enemys named: car, semi and bike. I have the logic of when the enemy will spawn and what enemy it will be. (I just have it print to the console of what vehicle it will be and what lane it will be in)

 function enemyFunction () : IEnumerator {
     var lane: int = Random.Range(1,4);
     var vehicleType: int = Random.Range(1,7);
     switch (vehicleType){
         case 1: //Semi
         case 2: 
             switch (lane){
                 case 1: 
                     Debug.Log("Semi, lane 1");
                     yield WaitForSeconds (3);
                     enemyFunction();
                   break;
                 case 2:
                     Debug.Log("Semi, lane 2");
                     yield WaitForSeconds (3);
                     enemyFunction();
                 break;
                 case 3:
                     Debug.Log("Semi, lane 3");
                     yield WaitForSeconds (3);
                     enemyFunction();
                 break;            
             }
         break;
         case 3: //bike
             switch (lane){
                 case 1: 
                     Debug.Log("Bike, lane 1");
                     yield WaitForSeconds (3);
                     enemyFunction();
                 break;
                 case 2:
                     Debug.Log("Bike, lane 2");
                     yield WaitForSeconds (3);
                     enemyFunction();
                 break;
                 case 3:
                     Debug.Log("Bike, lane 3");
                     yield WaitForSeconds (3);
                     enemyFunction();
                 break;            
             }
         break;
         case 4: //car
         case 5:
         case 6:
             switch (lane){
                 case 1: 
                     Debug.Log("Car, lane 1");
                     yield WaitForSeconds (3);
                     enemyFunction();
                 break;
                 case 2:
                     Debug.Log("Car, lane 2");
                     yield WaitForSeconds (3);
                     enemyFunction();
                 break;
                 case 3:
                     Debug.Log("Car, lane 3");
                     yield WaitForSeconds (3);
                     enemyFunction();
                 break;            
             }
         break;            
     } 
 }    
     
 enemyFunction();

I have this written on a master script that is connected to an empty game object. Would I want to have the actual code of spawning in this script, or would I have something that would refer to another script that is attached to the actual vehicle that I am spawning. I am completely lost on how to do this.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Kiloblargh · Mar 01, 2014 at 04:11 AM

You need these variables, which you fill in the Inspector;

 var bike : GameObject;
 var car : GameObject;
 var semi : GameObject;
 
 var lane1 : Vector3;
 var lane2 : Vector3;
 var lane3 : Vector3;

and this function:

 function Spawn (what : GameObject, where : Vector3) {
     var vehicle : GameObject = Instantiate (what, where, Quaternion.identity);
     }


Then you would just replace all those Debug messages with (for example):

 Spawn (car, lane2);


Btw, that's some fugly logic you've got going on. Who taught you to write recursive functions with nested switch/case statements anyhow? Look up InvokeRepeating, you can rewrite the whole thing much more neatly with that.

Comment
Add comment · Show 1 · 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 byurocks23 · Mar 01, 2014 at 10:30 PM 0
Share

About my logic, it works so I am fine with that. I have one other way I know of changing it but i figure it doesn't make a difference. Would putting all of it in a while loop that is set to true and remove the recursive functions be better. I feel this way could be less exact when it comes to ti$$anonymous$$g.

avatar image
0

Answer by iHaveReturnd · Mar 01, 2014 at 04:06 AM

A typical kind of thing to do with this is make this script have public variables that store the prefabs you want to spawn. You can make a prefab of the car for example, and then store that in a GameObject variable:

 var car : GameObject;

Then you can run the Instantiate function on that prefab to create the car. This script could call it, you wouldn't want to try to have that spawning script be attached to the enemy, as an enemy cannot create itself if it doesn't exist yet ;)

The instantiate documentation is here: https://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html And here is the example they give: Instantiate (prefab, Vector3(i * 2.0, 0, 0), Quaternion.identity);

So prefab would be your car variable, the Vector3 is the position you want it to create at, which you can set by getting another game object's position if you have a spawner, and the last part is the rotation of the spawned prefab.

If any of this needs clarification let me know!

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

21 People are following this question.

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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Object reference not set to an instance of an object 1 Answer

After instantiating an instance of a prefab and changing the value of a variable in a script attached to the prefab, that value is lost 1 Answer

Instantiating a random dropped consumable item from many cloned objects 1 Answer


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