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 Aggrojag · Apr 12, 2014 at 11:43 PM · c#instantiateontriggerenterienumeratorwont stop

Instantiation Wont Stop

So, I brute forced my way in to getting this script to function. I slightly understand why it works with how I set it up. However, I believe there must be a much better way to do this. The problem appears to be that once the script hits instantiate that it wants to do it before anything else and repeat on it. I made it so it turns the collider off, so it forces it to only shoot twice ( even though this should only be once) instead of infinitely. Any help is much appreciated here.

using UnityEngine; using System.Collections;

public class Shoot : MonoBehaviour {

 public GameObject bullet;
 
 public GameObject turret;
 
 private Vector3 turretLocation;
 
 private Quaternion rotation;

 private bool shot = false;

 public int waitTime;

 IEnumerator Start()
     
 {
     
     yield return new WaitForSeconds(waitTime);
     
 }
 
 
 
 IEnumerator OnTriggerEnter(Collider collision){
     if (shot == false){
         yield return new WaitForSeconds(waitTime);
         shot = true;
     }
     if (collision.tag == "player" && shot == true) {
         collider.enabled = false;
         turretLocation = turret.transform.localPosition;
         rotation = new Quaternion(0f,0f,0f,0f);
         Instantiate(bullet, turretLocation, rotation);
         shot = false;
     }
     collider.enabled = true;
 }
 

}

Comment
Add comment · Show 2
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 robertbu · Apr 12, 2014 at 11:47 PM 0
Share

I believe there must be a much better way to do this

You have not described what this script is attempting to do, so we would only be guessing about 'better way'. You should put a Debug.Log() statement just inside the OnTriggerEnter() function to see how many times it is getting called. And you should check to see how many objects have this script. There is nothing I can see in this script that would cause repeated Instantiates().

A couple of other points. For a (0,0,0) rotation you can use Quaternion.identity. Also your WaitForSeconds() in Start() is not doing anything...that is it will not delay any processing of OnTriggerEnter() or even Update() calls.

avatar image Aggrojag · Apr 13, 2014 at 12:34 AM 0
Share

There is one object with this script. It is a GameObject with a box collider that is set to act as a trigger. When the player is inside of this zone, I want it to instantiate a bullet. I've added a script on to the prefab of the bullet to make it move. So the box collider creates a bullet, and the bullet moves. However, it keeps creating bullets endlessly, until I added the collider.enabled portions of the script. I want it to create a bullet once every second or so. However, it just creates them endlessly without the collider.enabled portions of the script. As it's written, it actually creates two bullets.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Apr 13, 2014 at 01:14 AM

@PianoHandsThePerson - OnTriggerEnter() should only be called once at the time the object enters the collider. There is nothing I can see in this code that would cause a repeated Instantiate(). So to start, I recommend doing the Debug.Log() I mentioned above to see if OnTriggerEnter() is being called multiple times. Also in the upper corner of the hierarchy window there is a search field. Type in the name of this script to verify that it is only in the project once. And look at the bullet prefab to make absolutely sure this script is not on the prefab.

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 Aggrojag · Apr 13, 2014 at 01:32 AM 0
Share

So, it is definitely being called twice. I did the search for the name of the script, it's only on one object.. I'm absolutely confused on why this is happening. Also, I checked my pre-fab again. It's not on there either. If you have any other suggestions or ideas, I'd greatly appreciate the help. Thank you very much for the information thus far.

Edit: I found the source of the problem. Also, I believe I fixed it. I had a sphere that follows the player around, which is under the player in the heirarchy. The tag was NOT "player" however it was undefined. I changed the tag, and added if(collider.tag == "player") encasing the entire script, and now it is only firing once. Thank you so much!

Edit2: I can't seem to select your comment as the appropriate answer btw. Not sure how to close this out without writing out the answer myself, which seems... poor mannered.

avatar image robertbu · Apr 13, 2014 at 04:35 AM 0
Share

I converted my comment to an answer. I'm glad you found the problem.

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

Make enemy spawn only if the previous one spawned it out of the way. 0 Answers

OnTriggerEnter() not working 1 Answer

issue with making a moving object trap c# (SOLVED) 2 Answers

Distribute terrain in zones 3 Answers

instantiate within OnTriggerEnter, c# question. 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