I don't if I am stupid,but it confuses me really.
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);
}
}***
I am sorry,the script I write is this,if I click button quickly enough,some ball did not disappear.
public class RayShooter : $$anonymous$$onoBehaviour { private Ray ray; private Vector3 rayStartPoint; private Camera _camera; RaycastHit rayHit; GameObject ball;
// Use this for initialization
void Start () {
_camera =GetComponent<Camera>();
}
// Update is called once per frame
void Update ()
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
rayStartPoint=new Vector3(_camera.pixelWidth/2,_camera .pixelHeight/2,0);
ray=_camera.ScreenPointToRay(rayStartPoint);
Physics.Raycast(ray, out rayHit);
//To Show the ball on the Object hit;
ball = GameObject.CreatePrimitive(PrimitiveType.Sphere);
ball.transform.position = rayHit.point;
StartCoroutine("Creatball");
}
}
private IEnumerator Creatball()
{
yield return new WaitForSeconds(0.3f);
Destroy( ball);
}
}
Answer by Willliam · Mar 05, 2017 at 03:07 PM
If I write like this ,every ball is destroied ,and it is ok.
using UnityEngine; using System.Collections;
public class RayShooter : MonoBehaviour { private Ray ray; private Vector3 rayStartPoint; private Camera _camera; RaycastHit rayHit; // 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);
//To Show the ball on the Object hit;
// ball = GameObject.CreatePrimitive(PrimitiveType.Sphere);
//ball.transform.position = rayHit.point;
StartCoroutine("Creatball");
}
}
private IEnumerator Creatball()
{
GameObject ball = GameObject.CreatePrimitive(PrimitiveType.Sphere);
ball.transform.position = rayHit.point;
yield return new WaitForSeconds(0.3f);
Destroy( ball);
}
}
Your answer
Follow this Question
Related Questions
Rotate smoothly on sphere 2 Answers
Random spawn script 1 Answer
Planes to Sphere Issue 0 Answers
2D ball rolling animation 0 Answers