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
2
Question by TheShadyColombian · Oct 16, 2014 at 08:08 PM · c#instantiatevariablerandom

Spawn Random Enemy

I want to make a script that will pick one game object out of a few and instantiate it. also, i want it to be able to change the number of gam objects that i can assign with a variable, so that if i come up with another enemy, i can just change the variable. C# please!

Comment
Add comment · Show 1
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 Anxo · Oct 16, 2014 at 08:28 PM 0
Share

you should look at arrays in unity and the Random.range method. understanding the 2 will give you the power to do as you ask.

3 Replies

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

Answer by kamgru123 · Oct 16, 2014 at 08:44 PM

Something along this way should do what you ask for. In the editor add gameobjects to the array, then when you want to spawn the enemy just call SpawnRandom().

 class Spawner : MonoBehaviour
 {
     public GameObject[] objects;
     
     public void SpawnRandom()
     {
         Instantiate(objects[UnityEngine.Random.Range(0, objects.Length - 1)]);
     }
 }
Comment
Add comment · Show 4 · 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 TheShadyColombian · Oct 16, 2014 at 08:54 PM 1
Share

what's the [] ?

avatar image kamgru123 · Oct 16, 2014 at 10:09 PM 0
Share

that's an array

avatar image TheShadyColombian · Oct 17, 2014 at 02:58 AM 0
Share

Sweet, dude! This stuff is pure magic! Really appreaciate your hel. Big step in my game.

avatar image Hokkohono · Feb 22, 2015 at 11:14 PM 1
Share

I confirm: pure magic. Well done and thanks too !

This deserves to have a 100 votes so to be easily found on the web ^^"

To also have a random position, I used it this way :

     public GameObject[] objects;                // The prefab to be spawned.
     public float spawnTime = 6f;            // How long between each spawn.
     private Vector3 spawnPosition;
 
     // Use this for initialization
     void Start () 
     {
         // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
         InvokeRepeating ("Spawn", spawnTime, spawnTime);
     
     }
 
     void Spawn ()
     {
         spawnPosition.x = Random.Range (-17, 17);
         spawnPosition.y = 0.5f;
         spawnPosition.z = Random.Range (-17, 17);
 
         Instantiate(objects[UnityEngine.Random.Range(0, goodie.Length - 1)], spawnPosition, Quaternion.identity);
     }
     
avatar image
0

Answer by PixelZebra · May 11, 2015 at 11:06 PM

Hi there!

I really love this script. But being a beginner in the programming scene, I must ask, can someone tell me how to make my enemies move on an X axis upon their apparence on screen ? Thanks in advance :)

Comment
Add comment · Show 2 · 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 TheShadyColombian · May 11, 2015 at 11:11 PM 0
Share

How do you mean? If you want them to be instantiated on a random place in one axis and INA specific place on the other(s) then just instantiate it as a variable and set the variable's position on the random axis to Random.Range($$anonymous$$imum, maximum).

If I totally misunderstood that (I doubt I didn't) then post a comment (not an answer like with this question) and I can help you. I asked this a WHILE ago so I know a lot more now than I did before.

avatar image codytremblay · May 28, 2017 at 04:28 PM 0
Share

float maxspeed = 5;

Void Update(){

Vector3 pos = transform.position; // this gets the current position

pos.x += maxspeed * Time.deltaTime;

transform.position = pos; }

avatar image
0

Answer by OanhDv · Jul 17, 2017 at 02:51 AM

how to make spawn enemy with position camera

/

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 PinzonGames · May 03, 2020 at 09:38 PM 0
Share

You will put this script to camera . It will spawn enemy on click spacebar public GameObject enemy;

 void Update() {
      if(Input.GetKeyDown(KeyCode.Space)) {
 `Spawn();
 `}
 }
 
 void Spawn() {
      Vector3 pos = transform.position; // gets current location
      Instantiate(enemy, pos, Quaternion.identity);
 }








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

36 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 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

How to store a prefab in a variable 1 Answer

instantiate random prefabs based on player camera distance 1 Answer

Random array issue C# 2 Answers

Spawn enemies so they aren't spawned on top of each other (C#) 1 Answer

How to access value within instantiated class in different object? 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