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

Script went weird with 20 errors which don't make sense (unity)

Long story short, I was adding some code to my game I’m working on, but out of nowhere, 20 errors, they don’t make sense at all. At least as I see it, it’s a glitch, but how do I fix it?

here’s the code and the errors (there are more errors but all pretty much the same)
Errors redirect to random parts of the code that aren’t in any case an error, so I’m really wondering why it’s doing this and I can’t work on my game anymore

enter image description here

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

     using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerCore : MonoBehaviour

{
    // Health Variables
    public int MaxHealth = 2;
    public int Health;
    // Other Variables
    private bool DelayCheck = true;


    // Part about taking damage and taking damage when colliding with platforms that do damage and level borders
    public void TakeDamage(int Damage)
    {
        Health = Health - Damage;
    }

    public void Die()
    {
        Destroy(this.gameObject);
    }

    IEnumerator DelayBetweenDamage()
     {
        DelayCheck = false;
        TakeDamage(1);
        Debug.Log("Player took 1 damage.");
        yield return new WaitForSecondsRealtime(1);
        DelayCheck = true;

    }

    // Take Damage when touching platforms with the DMG_platform tag
    void OnCollisionStay2D (Collision2D col)
    {
        if (col.gameObject.tag == "DMG_platform" && DelayCheck == true) 
        {
            StartCoroutine(DelayBetweenDamage());
        }


    }

    // Take fatal damages when falling or going out of the map borders
    void OnCollisionEnter2D (Collision2D col)
    {
        if (col.gameObject.tag == "Level_Border")
        {
            TakeDamage(MaxHealth);
        }
    }

    // ----------------------------------------------------------------------------------------------------
    // Part about Bonus getting triggered and applying them to the player

    void OnTriggerEnter2D (Collision2D col)
    {
        if (col.gameObject.tag == "Power")
        {
            Destroy(col.gameObject);
            
            // if player touches Power Jump Bonus
            if (col.gameObject.name == "Power Jump")
            {
                \[SerializeField\] private Jump jump;
                jump.jumpVelocity = 22f;
            }
            
            // if player touches Power Health Bonus
            if (col.gameObject.name == "Power Health")
            {
                MaxHealth = 3;
                if (Health <= 2)
                {
                    Health = Health + 1;
                }
            }
        }
    }



    // Start is called before the first frame update
    void Start()
    {
        Health = MaxHealth;
        
    }

    // Update is called once per frame
    void Update()
    {
        // Dies if Health is less than 0 
        if (Health <= 0)
        {
            Die();
        }
        
        
    }




}

>Solution :

You broke the syntax structure.
I reformatted the code, here it is

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

public class PlayerCore : MonoBehaviour

{
    // Health Variables
    public int MaxHealth = 2;

    public int Health;

    // Other Variables
    private bool DelayCheck = true;


    // Part about taking damage and taking damage when colliding with platforms that do damage and level borders
    public void TakeDamage(int Damage)
    {
        Health = Health - Damage;
    }

    public void Die()
    {
        Destroy(this.gameObject);
    }

    IEnumerator DelayBetweenDamage()
    {
        DelayCheck = false;
        TakeDamage(1);
        Debug.Log("Player took 1 damage.");
        yield return new WaitForSecondsRealtime(1);
        DelayCheck = true;
    }

    // Take Damage when touching platforms with the DMG_platform tag
    void OnCollisionStay2D(Collision2D col)
    {
        if (col.gameObject.tag == "DMG_platform" && DelayCheck == true)
        {
            StartCoroutine(DelayBetweenDamage());
        }
    }

    // Take fatal damages when falling or going out of the map borders
    void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.tag == "Level_Border")
        {
            TakeDamage(MaxHealth);
        }
    }

    // ----------------------------------------------------------------------------------------------------
    // Part about Bonus getting triggered and applying them to the player

    void OnTriggerEnter2D(Collision2D col)
    {
        if (col.gameObject.tag == "Power")
        {
            Destroy(col.gameObject);

            // if player touches Power Jump Bonus
            if (col.gameObject.name == "Power Jump")
            {
                jump.jumpVelocity = 22f;
            }
            
            // if player touches Power Health Bonus
            if (col.gameObject.name == "Power Health")
            {
                MaxHealth = 3;
                if (Health <= 2)
                {
                    Health = Health + 1;
                }
            }
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        Health = MaxHealth;
    }

    // Update is called once per frame
    void Update()
    {
        // Dies if Health is less than 0 
        if (Health <= 0)
        {
            Die();
        }
    }

    [SerializeField] private Jump jump;
}
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