- Home /
destroy prefabs when they hit the player
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]
Answer by Subhanshu · Jul 29, 2017 at 01:11 PM
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.
Hi,thank you.but i have this error
hello @omaimah @Subhanshu made a small mistake He most likely wanted to write
void OnCollisionEnter2D(collision other)
{
if(other.collider.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.
}
}
Hi @omaimah . I am sorry, I forgot to put 'gameObject' before tag. It must be something like.
//Just add 'gameObject' between 'other' & 'tag'.
void OnCollisionEnter2D(Collision other)
{
if(other.gameObject.tag == "Player")
{
Destroy(gameObject);
}
}
Please make the above changes to your script. And Let me know if it works.!
@omaimah . You can try either of the above suggestions and let us know if it works.
Rather you should try both.
thanks for helping ,I tried both ways but it didn't work the fruit just pushed the player down you can see I but a temporary player. one more question ,should I change the player's tag or the prefab's (PS: I changed the prefab's tag ) @george095 @Subhanshu
[1]: /storage/temp/98838-111.png
Hi @omaimah .
It seems 'Boy' GameObject is your player as I can see in the Hierarchy,
If it is so, then assign 'Boy' GameObject with a tag "Player" which is in the Hierarchy.
If your player has a prefab then also you need to assign your prefab player with the same tag.
Now please make sure to remove 'Rigidbody2D' component from Fruits GameObjects. (Do not remove collider).
After this, make sure to set Player's Rigidbody2D gravity to '0' (Zero).
We can move our fruits downward using transform.Translate(); we don't need gravity.
See Code below with beady eyes from Head to Toe!
public class DestroyFruit : $$anonymous$$onoBehaviour {
//Here we can move down our fruits without Rigidbody.
//Using transform.Translate(); we can move it down.
private void Update()
{
//Vector2('X' direction, 'Y' direction);
//Since we want it to move downward that's why we'll use negative values in 'Y'.
//$$anonymous$$ultiplying our Vector2 values with Time.deltaTime will make it smoother.
transform.Translate(new Vector2(0.0f, -2f) * Time.deltaTime, Space.World);
}
//Here is the same code as we had before.
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Player")
{
Destroy(gameObject);
//Put some functionality to add score values here..
}
}
}
Try the above code & let me know.!
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Instantiate random platforms 0 Answers