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
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();
}
}
}
CodePudding user response:
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;
}
}
}
}
This code is wrong, do this instead :
if (col.gameObject.name == "Power Jump")
{
Jump jump;
jump.jumpVelocity = 22f;
}
you cannot declare a variable with a private field in a method. Moreover, I advise you to declare this variable as you did for the health variables
CodePudding user response:
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;
}
CodePudding user response:
The script below has been modified and fixed.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCore : MonoBehaviour
{
[SerializeField] private Jump jump;
// Health Variables
public int MaxHealth = 2;
public int Health;
// Other Variables
private bool DelayCheck = true;
// 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();
}
}
// 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 player touches Power Health Bonus
if (col.gameObject.name == "Power Health")
{
MaxHealth = 3;
if (Health <= 2)
{
Health = Health 1;
}
}
if (col.gameObject.tag == "Power")
{
Destroy(col.gameObject);
}
// if player touches Power Jump Bonus
if (col.gameObject.name == "Power Jump")
{
jump.jumpVelocity = 22f;
}
}
}