My code always comes up with this error Assets\Scripts\SpikeDestruction.cs(22,13): error CS0029: Can

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpikeDestruction : MonoBehaviour
{

    // Start is called before the first frame update
    void Start()
    {
      
    }

    // Update is called once per frame
    void Update()
    {

      
    }
    private void OnCollisionEnter2D (Collision2D collision)
    {
        if (collision)
        {
            Destroy(gameObject);
        }
    }

}

Ive followed some tutorials and they work but mine doesn’t i just want if it hits something it gets destroyed but theres always the error Assets\Scripts\SpikeDestruction.cs(22,13): error CS0029: but i don know how to fix it
Any suggestions

so what are you expecting line 22 to mean, if (collision) on its own does not make sense. what are you trying to check for?

You can’t use:

if (collision)

It isn’t true or false, you can use it like:

if (collision.transform.name == "Enemy")

if (collision.transform.tag == "Enemy")

Or just have Destroy(gameObject) without a condition.

1 Like

As @davidnibi hints at, Collision is not a unity object so it doesn’t have an implicit bool operator like Unity Objects do. When Unity calls OnCollision messages the collision object they pass will never be null inside the context of that method. you don’t need to check if its null.