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 theok_1989 · Apr 23, 2013 at 11:58 AM · spawnspawningspawnpointsspawning-enemies

Spawn Point

Hi guys, I have this spawn script that works fine, but I would like to add an OnTriggerEnter function where enemies only spawn when the player gets close to the spawnpoint. Can anyone help me do this? I would not be asking if i knew how to do it, but I'm getting desperate as it is the only thing not working properly. Cheers!

public class Spawn : MonoBehaviour { public GameObject zombie; public float delayTime = 4f;

 IEnumerator Start()
 {
     var obj = Instantiate(zombie, transform.position, transform.rotation) as  GameObject;
     yield return new WaitForSeconds(delayTime);
     StartCoroutine(Start());
 }

}

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
0

Answer by jakovd · Apr 23, 2013 at 01:09 PM

Put a collider component on an object that has this script, check IsTrigger checkbox on the component and put a tag "Player" on your players game object (in Inspector window, just above the Transform values).

 public GameObject zombie;
 
 public void OnTriggerEnter(Collider other)
 {
         if ((other.tag == "Player")) //checks if it is Player that entered this trigger
         {
          var obj = Instantiate(zombie, transform.position, transform.rotation) as                GameObject;
         }             
 }
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 TonyLi · Apr 23, 2013 at 01:15 PM

Here's one solution:

  1. Set the player's tag to "Player".

  2. Add a SphereCollider to your spawnpoint. Set the radius to the maximum distance at which it should detect the player. Check "Is Trigger".

  3. Change Spawn.cs to:

    public class Spawn : MonoBehaviour {

       public GameObject zombie; 
         public float delayTime = 4f;
         private const string PlayerTag = "Player";
         private bool isPlayerNearby = false;
      
         void Start() {
             StartCoroutine(Spawn());
         }
     
         IEnumerator Spawn() {
             while (true) {
                 if (isPlayerNearby) Instantiate(zombie, transform.position, transform.rotation) as  GameObject;
                 yield return new WaitForSeconds(delayTime);
             }
         }
      
         void OnTriggerEnter(Collider other) {
             if (string.Equals(other.tag, PlayerTag)) isPlayerNearby = true;
         }
      
         void OnTriggerExit(Collider other) {
             if (string.Equals(other.tag, PlayerTag)) isPlayerNearby = false;
         }
      
     }
     
    
Comment
Add comment · Show 9 · 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 theok_1989 · Apr 23, 2013 at 03:49 PM 0
Share

i don't why but something's gone wrong. i tried attaching the script to another spawnpoint and i got these two errors:

error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

and

error CS0542: `Spawn.Spawn()': member names cannot be the same as their enclosing type

please help!

avatar image jakovd · Apr 23, 2013 at 03:55 PM 0
Share

your script (class) is named Spawn and you have a method named the same in it. The method of a class with a same name is a special kind of method (constructor) thus you should avoid using the same name. Rename that Spawn method into something like SpawnEnemy. Remember to also change that name in StartCoroutine call in your Start method.

avatar image Bunny83 · Apr 23, 2013 at 09:16 PM 2
Share

@jakovd: StartCoroutine call is fine the way he has it, beside the name "Spawn". Using the string version should actually be avoided because the compiler can't help you when you mistype something or when you try to figure out who calls which functions.

avatar image jakovd · Apr 23, 2013 at 11:52 PM 1
Share

Ok. Thanks.. $$anonymous$$y design choice would be to call for spawing with InvokeRepeating in OnTriggerEnter and end it with CancelInvoke in OnTriggerExit. Seems simpler to understand than this infinite while loop and WaitForSeconds. $$anonymous$$y two cents.

avatar image TonyLi · Apr 24, 2013 at 12:30 PM 1
Share

I agree -- Unless you plan to do additional processing in the SpawnEnemy() loop, jakovd's suggestion is more elegant.

Show more comments

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

15 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

Related Questions

Spawning help 1 Answer

Enemy Spawn System with time and limit enemy in game 2 Answers

Spawn waves & wait till it's killed, spawn again. 1 Answer

Random spawnpoint where only one prefab can spawn! 1 Answer

How to spawn enemies at different locations and avoid overlapping each other 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