I want to Destroy the gameobject that is in the prefabs when they touch the player I have this script I tried it when the gameobject wasn’t in the prefabs and it worked,thank you .
![alt text][1]
I want to Destroy the gameobject that is in the prefabs when they touch the player I have this script I tried it when the gameobject wasn’t in the prefabs and it worked,thank you .
![alt text][1]
Hi @omaimah .
1. Here we will create a separate script to destroy your prefabs.
**2. Name it ‘DestroyFruitPrefab’. (As per your desire) **
**3. In this script we will write few lines of code. **
void OnCollisionEnter2D(collision other)
{
if(other.tag == "Player")
{
//Apply any visual effects you want to render here when collision occurs.
Destroy(gameObject);
//This will destroy all your prefabs which has this script attached.
}
}
4. Attach this script to all your prefabs that you want to destroy. Remove previous script for a while.
5. Now just make sure to allot a ‘Tag’ to your Player GameObject in the Hierarchy. Tag your player with a tag - “Player”.
If ‘Player’ tag isn’t available then look at the very bottom of drop down list. There’s an option of ‘Add Tag’. Click on it and create a new tag naming it ‘Player’. Then finally allot it to your Player GameObject.
6. It is worth noting to check for the spelling on the player’s tag in the Editor and the same spelling in the script. (Case Sensitive).
7. Once we’re done, save the script and run again.
I hope it will work.!
EDIT : You can see the image here, I used the script in my last comment and it is working fine.
After Colliding with Player, the player isn’t going down because our fruits are no longer being affected by physics forces. As we removed ‘Rigidbody’ component.