- Home /
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;
}
}
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.
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.
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.
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.
I converted my comment to an answer. I'm glad you found the problem.