Why does the Sprite not get destroyed 2D unity - c#

Right now we work on a Infinity-Runner, and i got this weird bug. After something leaves the Screen it will be catched by a collider that destroys everything. And it works...almost. It will destroy the ground, Background stuff, enemys you jumped over but NOT the newly implemented "roadblocks".
Here is a picture how it looks like, the green placeholder is the roadblock-thingy
http://s4.postimg.org/8uaorv7ot/Bug.png
hope this might help in visualizing what im saying^^.
The Script i use for the Destroyed (the green box collider) is:
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Debug.Log ("Break is gonna happen");
Debug.Break();
}
else if (other.gameObject.transform.parent)
{
Destroy(other.gameObject.transform.parent.gameObject);
}
else
{
Destroy(other.gameObject);
}
}
and this is the script for the roadblocks:
void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.tag == "Player")
{
Debug.Break ();
}
}
so basically nothing that would ever disrupt the DestroyerScript. Its even far more simple than the script on the enemys...but they get destroyed.
Thanks in advance for your help, i can provide more information if need.
(ohh, and all the art in this pic is placeholderstuff^^)

You can try to use OnTriggerExit2D to trigger when your player leave the collider. And makesure you added a suitable collider2d to your sprite before
http://docs.unity3d.com/ScriptReference/Collider2D.OnTriggerExit2D.html
I'm not use it before but I think it might works

Related

can someone explain to me why my code dosen't work

i was making code for a project (i'm new in c#) and i'm so confused as to why my code dosent work
the code should make it so when a collider with the tag "floor" collides with my player it should change a variable,
no errors are coming up but the variable dose not change , here's the code where the problem is
void OnCollisionEnter2D()
{
if (GetComponent<Collider2D>().tag == "FLOOR")
{
jump = false;
Debug.Log("ITS A FLOOR");
}
}
Try this:
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "FLOOR")
{
jump = false;
Debug.Log("ITS A FLOOR");
}
}
My experience is mostly in 3D environments so I'm really not sure if I'm missing something, but your attempt to get a component doesn't make sense to me for two of reasons:
You didn't mention for which game object you would like the collider
The tag is found on the game object and not on the collider
Considering you want the variable jump to change. Debug with a breakpoint at the line
jump=false
Make sure the program reaches that line and make sure the scope of the variable is what you expect.
Going off the difference between "floor" in your description and then in your code, the string you are searching for may have different casing: "floor" != "FLOOR".
Try the following, if you need a case invariant string comparison:
if (string.Equals(GetComponent<Collider2D>().tag,"FLOOR", StringComparison.InvariantCultureIgnoreCase))
There is no void OnCollisionEnter2D() in Unity, only void OnCollisionEnter2D(Collision2D collision) where Collision2D collision is the collision that your object (player) entered.
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "FLOOR")
{
jump = false;
Debug.Log("ITS A FLOOR");
}
}
Aight, answers above already told the answer to the problem
void OnCollisionEnter2D()
{
if (GetComponent<Collider2D>().tag == "FLOOR")
{
jump = false;
Debug.Log("ITS A FLOOR");
}
}
Is this code attached to the player? Or to the floor? I assume it's attached to the player, in this case GetComponent() will try to get the Collider2D from the player's entity or whatever, but not from the collision
And you didn't declare the Collision2D parameter. Intellisense usually knows Unity's methods, might help you a lot
So, the right way to access tag from the collision would be
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "FLOOR")
{
//your code
}
}
Hope you'll get around

Lives Implementation in Unity2D

I begun the trek into Unity2D development a few months ago, so i'm still fairly new to all the unity engine jargon. In my game, I decided to implement the use of 'Lives'. I scripted out what (in my eyes) should work but every time the player dies instead of decrementing the lives counter and restarting it at the scene, it immediately loads the scene 1 (i.e. the GameOver screen) Is my logic here on this page incorrect or is there just a better way overall to handle lives than PlayerPrefs? (ALSO: Its worth mentioning that the Lives are instantiated/sent to playerprefs in the player script, I don't figure I need to include that one here)
This is the main block of code on my destroyer objects' script to account for 'death':
public static int lives;
void Start()
{
lives = PlayerPrefs.GetInt("lives");
}
void OnTriggerEnter2D(Collider2D other)
{
//If the trigger happens to be tagged as a 'Player', does this.
if (other.tag == "Player")
{
lives--;
if (lives < 0)
{
PlayerPrefs.DeleteAll();
SceneManager.LoadScene(1);
} else
{
SceneManager.LoadScene(0);
PlayerPrefs.SetInt("lives", lives);
}
}
if (other.gameObject.transform.parent)
{
Destroy(other.gameObject.transform.parent.gameObject);
}
else {
Destroy(other.gameObject);
}
}
As I mentinoned in comment, I think you forget to swithOn isTrigger field from Inspector which is under the Collider area.
Probably you thought like you are using onCollisionEnter. Also if you check the differences between OnTriggerEnter and OnCollisionEnter it will be very helpful.
As #Ugur Tufekci answers, But make sure to add BoxCollider2d not Box Collider.The simple box collider is 3d but your are detecting that onTriggerEnter2D that means 2d box collider has to be added

Unity2D: ForceMode2D.Impulse dragging player back

I have a script attached to a tagged object that pushes my player on trigger. It works perfectly, however my player (after collision) gets dragged back to the tagged object and continues bouncing. How would I stop this. All I want is for my player to get pushed back by the tagged object for a short amount of distance and stay there. Could anyone help me with this?
This is my script:
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Bouncy object")
GetComponent<Rigidbody2D>().AddForce(transform.right * 15, ForceMode2D.Impulse);
}
Try using a vector in your AddForce() method rather than affecting the transform directly:
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Bouncy object")
GetComponent<Rigidbody2D>().AddForce(new Vector2(15, 0), ForceMode2D.Impulse);
}

How do I detect if a sprite is touching another sprite

How do I determine if a sprite (Sprite1) if on top of (Sprite2)
I need this to switch levels when you touch another sprite with your sprite.
I tried:
void OnCollisionEnter2D(Collision2D other)
{
//code to run
}
But this made it so that everything it touched ran the code. :(
You should attach the script that contains oncollisioneneter to your sprite1 game object for example and in your sprite 2 game object add a tag like sp2 or whatever you want , then in your script when collision happens you check if the object that sprite one collided with is sprite 2 you check it with tag property of sprite 2 like this
void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.tag == "sp2")
{
//put your change levelcode here
}
}
Your halfway there. Right now you are running the code whenever the sprite collides with another 2D collider. What you then need to do is determine if that collider is the one attached to the particular sprite we want to trigger the code.
Tag the object containing your level exit, and check for the tag when you get a hit.
void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.tag == "levelExit")
{
Debug.Log("next level");
}
}

Identify a selected enemy prefab to play death particle system

I am trying to play a particle effect when an enemy is killed but it seems to play on a randomly selected one rather than the one that was hit. However the enemy that was hit still disappears and still add points to the score.
At the moment I have three scripts to carry this out (All have been shortened so I'm only showing the relevant code):
One which is attached to boxes that are thrown at enemies that detects if they have collided with an enemy prefab.
void OnCollisionEnter (Collision theCollision) {
if (canProjectileKill == true) {
// If the projectile hits any game object with the tag "Enemy" or "EnemyContainer".
if (theCollision.gameObject.tag == "Enemy") {
GameObject.Find("EnemyExplosion").GetComponent<enemyDeath>().ProjectileHasHitEnemy();
// Destroy the projectile itself.
Destroy (gameObject);
// Destroy the game object that the projectile has collided with (E.g. the enemy).
Destroy (theCollision.gameObject);
GameObject.Find("Manager").GetComponent<projectileSpawner>().deleteProjectile();
}
}
}
Another that is attached to the enemy prefabs which detects if they have been hit by a box.
void OnCollisionEnter (Collision theCollision) {
if(theCollision.gameObject.tag == "Projectile") {
GameObject.Find("EnemyExplosion").GetComponent<enemyDeath>().EnemyHasBeenHit();
}
}
I then run an if statement asking if both the box has hit the enemy prefab AND if the enemy prefab has been hit by the box in an attempt to identify a single prefab rather than all of them. However this still doesn't work.
public bool HasProjectileHitEnemy;
public bool HasEnemyBeenHitByProjectile;
void Start () {
gameObject.particleSystem.Stop();
HasProjectileHitEnemy = false;
HasEnemyBeenHitByProjectile = false;
}
public void ProjectileHasHitEnemy () {
// From projectile.
HasProjectileHitEnemy = true;
}
public void EnemyHasBeenHit () {
// From enemy.
HasEnemyBeenHitByProjectile = true;
PlayParticleSystem();
}
public void PlayParticleSystem () {
if (HasEnemyBeenHitByProjectile == true && HasProjectileHitEnemy == true) {
gameObject.particleSystem.Play();
HasProjectileHitEnemy = false;
HasEnemyBeenHitByProjectile = false;
}
}
}
I am aware this is a long question but I have been stuck on this for over a week, so any help would be much appreciated. Thank you :)
I'm not sure what kind of object EnemyExplosion is, but your problem seems to be the search for this object. During OnCollisionEnter you know exactly between which objects the collision occurred. But now you're starting a search for any object that is called EnemyExplosion. That's the reason your particles appear random.
Update:
Ok, with your structure something like that
EnemyContainer
- EnemyExplosion
- Particle System
- EnemyModel
- Collider
If EnemyModel contains the collider, you can get to EnemyExplosion and finally enemyDeath the following way.
var explosion = theCollision.transform.parent.gameObject.GetComponent<EnemyExplosion>();
explosion.GetComponent<enemyDeath>().ProjectileHasHitEnemy();
Now that you're accessing the correct object, you can remove some of your double checks and rely on one collider event.
I seem to have found a way around this. Instead I've just set it to instantiate the particle system whenever an enemy detects that it has collided with a projectile. I then use a Coroutine to delete the particle system 2 seconds after.

Categories