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
1
Question by forfor · Dec 29, 2013 at 05:41 AM · instantiatetriggerspawnnoobenter

Spawn Point Problems

Hi guys so I am new to coding and need some help. This is the first code I've attempted to write on my own, and I cant seem to get it to work. Basically the idea is that when the player enters a certain box, it waits a period of time then spawns one monster. The problem is that A: It seems to spawn random numbers of monsters in the time it takes to process the code that tells it to stop, so one time it will spawn 50-100 another it will spawn 3 etc. B: Anything will activate the trigger that starts the spawning. I can activate it by shooting it with my gun, or the enemies that spawn will activate it and create an infinite loop. I tried to follow the exact coding that someone else online wrote to specify only the player could activate it but with no effect. The wait time function works between spawns, luckily.

 var fight : boolean;
 var minWait : int;
 var maxWait : int;
 var waitTime : int;
 var spawn : boolean;
 var enemiesSpawned : int;
 var enemy : GameObject;
 var player : GameObject;
 var breach : boolean;
 
 function Start()
 {
 fight = false;
 spawn = false;
 breach = true;
 minWait = 5;
 maxWait = 20;
 waitTime = Random.Range(minWait, maxWait);
 player = GameObject.Find("Player");
 }
 
 function Update()
 {
 if(fight == true && breach == true)
 
 {
 Encounter();
 }
 }
 function Encounter()
 {
 spawn = true;
 if(spawn == true)
 {
 Invoke("Spawn", waitTime);
 }
 }
 
 function Spawn()
 {
 if(spawn)
 Instantiate(enemy, transform.position, transform.rotation);
 enemiesSpawned +=1;
 NewWaitTime();
 spawn = false;
 breach = false;
 }
 
 function NewWaitTime()
 {
 waitTime = Random.Range(minWait, maxWait);
 yield WaitForSeconds(waitTime);
 Reset();
 }
 
 function Reset()
 {
 breach = true;
 }
 
 function OnTriggerEnter(other : Collider)
 {
 if(other.gameObject.tag == "Player")
 {
     fight = true;
 }
 }
 function OnTriggerExit(other : Collider)
 {
 if(other.gameObject.tag == "Player")
 {
     fight = false;
 }
 }
Comment
Add comment · Show 6
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 KellyThomas · Dec 29, 2013 at 06:18 AM 1
Share
  • You currently have no curly braces for the if statement in Spawn(). This means that the condition only applies to the following line, any subsequent lines to be executed regardless.

  • You find the "Player" object in Start(), if the find operation succeeds (you can confirm by watching the inspector as it runs) then you can use "`if(other.gameObject == player)`" in the OnTriggerEnter() and OnTriggerExit() methods.

Apart from those points I'm finding it hard to read the purpose behind the logic you have built. I'm left with many questions: What is breach?; Why yield before calling Reset()?; Should enemiesSpawned be checked to deter$$anonymous$$e when to stop spawning?....

I would suggest mentally stepping though your ideal logic flow, and then when you have that solidified check the code to confirm it matches the ideal.

avatar image forfor · Dec 29, 2013 at 06:35 AM 0
Share
 function Spawn()
 {
 if(spawn)
 {
 Instantiate(enemy, transform.position, transform.rotation);
 enemiesSpawned +=1;
 NewWaitTime();
 spawn = false;
 breach = false;
 }
 }
avatar image forfor · Dec 29, 2013 at 06:35 AM 0
Share

Like that? Everything else works except the random quantities of enemies now.

avatar image forfor · Dec 29, 2013 at 06:40 AM 0
Share

I mostly set up breach as an additional step to prevent the massive enemy spawns, and i chose to yield before reset because the idea was that as long as breach wasnt true the trigger wouldnt happen that way i wouldnt have to spend forever trying to figure out how to contradict the OnTriggerEnter function. also I dont really know how to code the check for deter$$anonymous$$ing how many to spawn. XD

avatar image forfor · Dec 29, 2013 at 06:42 AM 0
Share

in the trigger i wrote via a tutorial, Instantiate only spawned one, and didn't need a check so I figured that was just how that command worked XD

Show more comments

1 Reply

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

Answer by KellyThomas · Dec 29, 2013 at 06:48 AM

Indenting will help both yourself and others read your code.

One way to put a hard limit on the number spawned would be like this, just define maxEnemies as you see fit:

 function Spawn()
 {
     if(spawn && enemiesSpawned < maxEnemies)
     {
         Instantiate(enemy, transform.position, transform.rotation);
         enemiesSpawned +=1;
         NewWaitTime();
         spawn = false;
         breach = false;
     }
 }

If you wanted each wave to start fresh you would then set enemiesSpawned back to zero in OnTriggerEnter().

Comment
Add comment · Show 5 · 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 KellyThomas · Dec 29, 2013 at 06:57 AM 0
Share

Also note:

 function Update()
 {
     if(fight == true && breach == true)
     {
         Encounter();
     }
 }

 function Encounter()
 {
     spawn = true;
     if(spawn == true)
     {
         Invoke("Spawn", waitTime);
     }
 }

Could all be simplified to:

 function Update()
 {
     if(fight == true && breach == true)
     {
         spawn = true;
         Invoke("Spawn", waitTime);
     }
 }
avatar image forfor · Dec 29, 2013 at 07:05 AM 0
Share

thank you :) it should work fine now, once I can get it to start registering new integers, which isnt happening for some reason.

avatar image forfor · Dec 29, 2013 at 07:24 AM 0
Share

got it working :) i think it just took a while to update the script :)

avatar image forfor · Dec 29, 2013 at 07:24 AM 0
Share

thank you for all your help :)

avatar image KellyThomas · Dec 29, 2013 at 07:26 AM 0
Share

no problems

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

19 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

Related Questions

Spawn OnTriggerEnter 0 Answers

Spawning objects on trigger does not work as expected 2 Answers

Issue With Spawning Enemies (javascript) 2 Answers

Instantiated (clone of) prefab, doesn't trigger another object's collider when inside it. 2 Answers

Want a script that can instantiate an object when MOVING inside the trigger 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