public class RayShooter : MonoBehaviour {
private Ray ray;
private Vector3 rayStartPoint;
private Camera _camera;
RaycastHit rayHit;
private GameObject ball;
// Use this for initialization
void Start () {
_camera =GetComponent<Camera>();
}
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonDown(0))
{
rayStartPoint=new Vector3(_camera.pixelWidth/2,_camera .pixelHeight/2,0);
ray=_camera.ScreenPointToRay(rayStartPoint);
Physics.Raycast(ray, out rayHit);
ball = GameObject.CreatePrimitive(PrimitiveType.Sphere);
ball.transform.position = rayHit.point;
StartCoroutine("Creatball");
}
**}
The Script below I write down is to make a Sphere on the RayCastHit point and destroy it 0.3 seconds later.but when I click the mouse quickly enough,some spheres are not destroied,why ? When I move the script “ball = GameObject.CreatePrimitive(PrimitiveType.Sphere); ball.transform.position = rayHit.point;” into the coroutine method(add a script in front :”GameObject ball;“ )and delete the script “private GameObjet ball”.it is ok.everyball is destroid,why**
private IEnumerator Creatball()
{
yield return new WaitForSeconds(0.3f);
Destroy( ball);
}
}***