#Not sure if this was seen since it was

1 messages · Page 1 of 1 (latest)

true cairn
#

What is EnemyOrb set to in the first method?

#

Why not use objectBumpingIntoThis.GetComponent<EnemyOrb>().Kill() ?

normal path
#

For ease of access I am sending the code here:

Killer object's code:

    {

        if (objectBumpingIntoThis.gameObject.layer == 9)
        {
            EnemyOrb.Kill();
        }
        Destroy(objectBumpingIntoThis.gameObject);
    }```


EnemyOrb object's code"
```public void Kill()
    {
    spriteRenderer.enabled = false;
    Destroy(EnergyOrbs.Instantiate(particlesPrefab, 
transform.position, particlesPrefab.transform.rotation), 1f);
    }```
normal path
true cairn
#

You could rewrite the first method as:

var enemyOrb = objectBumpingIntoThis.GetComponent<EnemyOrb>();
if (enemyOrb)
{
    enemyOrb.Kill();
    Destroy(enemyOrb.gameObject);
}
#

Though I would use Collision2D collision instead of Collision2D objectBumpingIntoThis

normal path
#

I used objectBumping... to help me remember what it is.

true cairn
#

So it becomes:

private void OnCollisionEnter2D(Collision2D collision)
{
    var enemyOrb = collision.gameObject.GetComponent<EnemyOrb>();
    if (enemyOrb)
    {
        enemyOrb.Kill();
        Destroy(enemyOrb.gameObject);
    }
}
#

In my opinion, it is not a thing but data about a collision that occurred, and you call .gameObject or .rigidBody or .collider to get the thing that collided with this collider.

#

But stick with whatever makes it easiest for you to remember and understand your code.

normal path
#

👍

#

That worked like a charm! Thanks!

I still have the transform.position problem though.

true cairn
#

um...

#

The Kill method is being called on the right game object now, right?

#

Put a Debug.Log call in there and log the gameObject.name and transform.position...

normal path
#

It seems to be working more... I'll poke around a bit. Not sure why your way fixed a seemingly unrelated problem, but it did. Thanks.

true cairn
#

I suggested my change primarily to make sure it was calling Kill on the right game object, since I did not know where your EnemyOrb variable was being set.

#

And since you were being given the object right there, I thought you should use what you were given.

normal path
#

Is there a reason discord is eating my messages?

#

I was gonna say EnemyOrb was being set manually in the inspector, and I assume that's why it was using the prefab.

#

Anyway, it's working the way I want now. You helped a ton. Thanks!