Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I fix } expected error in Unity C#?

I have a little problem with my C# code, so I have this simple PlayerHealth code here and Unity always gives me this error: Unity Bracket Error

Code:

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

public class PlayerHealth : MonoBehaviour
{
    public float health = 100;
    public float damage = 10;


    void OnCollisionEnter(otherObj Collision) {
        if (otherObj.tag == "Bullet") {
            health = health - damage;
            if (health < 0)
            {
                Destroy(gameObject);
            }
        }
    }
}

I appreciate your help! 😀

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Check your OnCollisionEnter function, the parameter is wrong.

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

public class PlayerHealth : MonoBehaviour
{
    public float health = 100f;
    public float damage = 10f;

    void OnCollisionEnter(Collision otherObj) 
    {
        if (otherObj.gameObject.tag == "Bullet") 
        {
            health = health - damage;
            if (health < 0)
            {
                Destroy(gameObject);
            }
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading