- Home /
Deleting a prefab with onclick/touch
Hi,
I am trying to make a game where an object falls from the top(out of view) to the bottom of the screen and i am trying to implement that when the player touches the screen the falling object is removed from the game and in turn adds a point to the player's score.
I have created the gameObject as a prefab and added the following code but nothing happens when i click on the object;
public class OnClickDestroy : MonoBehaviour {
void OnMouseDown()
{
Destroy (gameObject);
}
}
i added this code into the inspector for the prefab, but i woundered if the code is not working because i am using 'Destroy (gameObject);' in the code and the item falling is a prefab?
thank you
$$anonymous$$ake sure it has a collider attached, and it is not a trigger! You don't want to destroy the prefab only the instance on the scene. Also look around for object pooling to improve performance. Cheers!
Well try this and place it on your Object.
void Update()
{
if (Input.Get$$anonymous$$ousButtonDown($$anonymous$$eyCode.$$anonymous$$ouse0))
{
destroy (gameObject)
}
}
thank you for your answer it really helped and it is finally working
glad i could help :-) i thought unity/answer helped me alot, so i'm giving something back :-)
Answer by BlakeSchreurs · Sep 29, 2017 at 12:33 PM
A prefab is basically a copy of a GameObject that's been saved to file.
A game object in your game may be spawned (instantiated) from a prefab, but none of the GameObjects in your game ARE the prefab (it's just a save file, after all).
OnMouseDown requires a collider, so check to make sure your GameObject has one of those. Also, make sure there aren't any objects (even transparent ones) between the camera and the object.
Hi,
Thank you for replying.
The prefab I have is a star and I have a rigidbody2d and a polygon collider. The only thing I use for my main camera is a background image.
This is my spawning code incase there is something in it thats wrong.
public GameObject star;
// Use this for initialization
void Start () {
InvokeRepeating ("Spawn", 2.0f, 0.3f);
}
// Update is called once per frame
void Spawn () {
Instantiate(star, new Vector3(Random.Range(-10,25),5,5),Quaternion.identity);
}
}
Spawn script looks good. On$$anonymous$$ouseDown looks good... I'm guessing you have an issue with your collider, or maybe your click script isn't attached to your prefab. When you pause your game, and look at a star in the scene view, you should see a green outline which is the polygon collider's representation of the object.
I've created a micro-project as a reference which shows things working. https://www.dropbox.com/s/5bjl5t57dbim811/CollisionTest%20-%20Copy.zip?dl=0
Thank you this really helped me a lot. I have finally managed to get it to work with your help and a lot of tinkering but it works and I am so happy as it was taking up so much time.
Your answer
Follow this Question
Related Questions
How to load instances of a prefab to an array c# 2 Answers
I want to open loot chest prefabs individually in the scene 3 Answers
triggering an animation on click? 1 Answer
Button only works once 1 Answer
Multiple Cars not working 1 Answer