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 zefrof · Oct 24, 2012 at 11:47 PM · c#enemyspawninginstantiating

Changing spawning position with C#

I have a limited knowledge of coding so please bear with me. I have this code:

 using UnityEngine;
 using System.Collections;
 
 public class EnemySpawning : MonoBehaviour {
     public int SpawnCount;
     
     public bool Position1;
     public bool Position2;
     
     public GameObject SpwanPosition;
     public GameObject Enemy;
 
     // Use this for initialization
     void Start () {
         SpawnCount = 2;
         
         Position1 = false;
         Position2 = false;
         
         SpwanPosition = GameObject.Find("EnemySpawn");
         Enemy = GameObject.Find("Enemy");
         
     }
     
     // Update is called once per frame
     void Update () {
         if(SpawnCount > 0)
             Instantiate(Enemy, SpwanPosition, Quaternion.identity);
             SpawnCount -= 1;
     }
     
     void ChangeSpawn () {
         if(GameObject.Find("Enemy").transform.position == 0, 0, 0) {
             Position1 = true;
         }
         
         if(Position1 = true)
             SpwanPosition = GameObject.Find("EnemySpawn2");
     }
 }

I have 2 Gameobjects, "Enemy" and "EnemySpawn." Enemy is well, the enemy in the game. EnemySpawn is an empty Gameobject when the enemy spawns, there are currently 2 in the scene. I want it to spawn an enemy at the first EnemySpawn and then spawn another enemy at the second EnemySpawn. Any help is appreciated.

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 Memige · Oct 25, 2012 at 12:21 AM

Okay, looks like there is a decent amount of love this script could use. First off we need to address the way you are creating these enemies. Instantiating objects off of already instantiated objects is non-ideal, it can create issues if you destroy the original. Instead you will want to create a prefab for your enemy. Next, GameObject.Find is a hog and we should avoid it anytime we can. Instead, we can create a public GameObject array to store all of our spawn points in, and then iterate through it in script to use them.

 using UnityEngine;
 using System.Collections;
 
 public class EnemySpawning : MonoBehaviour {
     public int SpawnCount;
 
     public bool Position1;
     public bool Position2;
 
     public GameObject[] SpawnPosition;
     public GameObject Enemy;
 
     // Use this for initialization
     void Start () {
        SpawnCount = 2;
 
        Position1 = false;
        Position2 = false;
     }
 
     // Update is called once per frame
     void Update () {
        if(SpawnCount > 0)
        {
          GameObject.Instantiate(Enemy, SpawnPosition[SpawnCount-1].transform.position, SpawnPosition[SpawnCount-1].transform.rotation);
          SpawnCount--;
        }
     }
 }

This will now require some addition setup in your editor. After you have created your Enemy prefab, just drag and drop it on the Enemy variable in the inspector for the object that has your EnemySpawning script. Then select both EnemySpawn objects in your scene Hierarchy and drag and drop then onto the SpawnPosition variable again in the inspector for the object that has your EnemySpawning script.

This has the added benefit of rotating your enemy to the same orientation as the EnemySpawn object, allowing you to easily update in the editor the direction a given Enemy will face when spawned.

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
avatar image
0

Answer by speedything · Oct 25, 2012 at 12:25 AM

A few points. First, when comparing a value (as opposed to setting it) you need two "=" sign. -

    if(Position1 = true) // This should be (Position1 == true)

You also should be careful of this

     if(GameObject.Find("Enemy").transform.position == 0, 0, 0)

Floats are very rarely exactly on an integer value. I'd either allow a range (between say -0.5 and 0.5 on each axis), test distance from the point, or use a collider to trigger if something is in it. The last is my preferred as its more intuitive and easy to edit.

Finally,

       SpwanPosition = GameObject.Find("EnemySpawn2");

You have a typo "Spwan", but you also will with Unity hunting down the game object every time you reassign it, Far better to set it in Start and call it later..

 //In Start
 GameObject spawn1 = GameObject.Find("EnemySpawn1");
 GameObject spawn2 = GameObject.Find("EnemySpawn2");
 
 //Spawn enemy
 if(position1 == true)
 {
    //Instantiate at spawn2.transform.position
 }
 else if (position2 == true)
 {
    //Instantiate at spawn2.transform.position
 }
 

 
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 dwalker350 · Aug 23, 2015 at 12:19 AM 0
Share

Unity does account for small variations when comparing vectors. if (GameObject.Find("Enemy").transform.position == Vector3.zero) will work correctly.

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

11 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

Related Questions

How would I go about enemy spawning? (C#) 1 Answer

Trying to make a spawning enemy code 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to make my enemies spawn at spawn point, but not randomly? 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