#Not sure if this was seen since it was
1 messages · Page 1 of 1 (latest)
What is EnemyOrb set to in the first method?
Why not use objectBumpingIntoThis.GetComponent<EnemyOrb>().Kill() ?
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);
}```
That would make more sense. This way (I assume) I don't have to manually assign EnemyOrb in the inspector.
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
I used objectBumping... to help me remember what it is.
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.
👍
That worked like a charm! Thanks!
I still have the transform.position problem though.
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...
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.