Hi.
How can I delete this bullet GameObject if a new one is spawned? There should always just be one in the scene. I tried doing something here, but I ended up deleting my whole character...
using UnityEngine;
using System.Collections;
public class Shooter : MonoBehaviour {
public GameObject bullet = null;
public float bulletSpeed = 500f;
public Transform target;
public float MoveTowardsSpeed;
public bool HookActive = false;
private void Update ()
{
if (Input.GetButtonDown ("Fire1"))
{
if (HookActive == true)
{
GameObject.Destroy (bullet);
HookActive = false;
}
HookActive = true;
GameObject shot = GameObject.Instantiate (bullet, transform.position + (transform.forward * 2), transform.rotation) as GameObject;
shot.GetComponent().AddForce (transform.forward * bulletSpeed);
float step = MoveTowardsSpeed * Time.deltaTime;
transform.position = Vector3.MoveTowards (transform.position, target.position, step);
}
}
}
↧