- Home /
when prefabs are spawned I want to touch them to destroy. help?
the issue I am having is this- when I touch the spawned game object it gets destroyed, but I want it to spawn another sprite before it it destroyed when the raycast2d hits it. I tried send message, but because it is an instantiated prefab this method did not work and returned an error in the console. What I am trying to do in pseudo code is this. Green balloon is spawned, when touch/click is pressed, green balloon spawns green balloon popped sprite and the green balloon is destroyed. So when touch is pressed on game object, spawn prefab at the same location and destroy the game object which is touched. I'm sorry if this is a really simple process, but I'm not sure how to get this to work given I am new. Thanks in advance for any further help regarding the solution.
Is the code that manages the raycast on the Green balloon, or one some other script like playerInput, etc.?
Call 'Instantiate' method for popped sprite before calling 'Destroy' on green baloon sprite. Instantiate(poppedBaloon, transform.position, trasform.rotation); Destroy();
i have this code on the main camera in the scene, when i touch/click the balloon it gets destroyed, but maybe i did it wrongly? i'm really not sure because I want that when i touch the balloons they get destroyed but instantiate the balloon pop sprite before being destroyed in the same location that the balloon was.
function Update () {
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
var hitm : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if(hitm.collider != null)
{
Destroy (hitm.collider.gameObject);
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began){
if(hitm.collider != null)
{
Destroy (hitm.collider.gameObject);
}
}
}
}
Your answer
Follow this Question
Related Questions
problem whit Instantiate Prefabs position. 0 Answers
Deleting a GameObject 2 Answers
Cannot destroy child object in prefab- Error 1 Answer
Solved: Destroy command not working 1 Answer
Parenting an instantiated prefab. 1 Answer