長話短說,我在我正在開發的游戲中添加了一些代碼,但是突然出現了 20 個錯誤,它們根本沒有意義。至少在我看來,這是一個小故障,但我該如何解決呢?
這是代碼和錯誤(有更多錯誤,但幾乎相同)錯誤重定向到代碼的隨機部分,在任何情況下都不是錯誤,所以我真的想知道為什么它會這樣做,我可以'不再玩我的游戲了

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();
}
}
}
uj5u.com熱心網友回復:
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;
}
}
}
}
此代碼錯誤,請改為:
if (col.gameObject.name == "Power Jump")
{
Jump jump;
jump.jumpVelocity = 22f;
}
您不能在方法中宣告具有私有欄位的變數。此外,我建議您像宣告健康變數一樣宣告此變數
uj5u.com熱心網友回復:
你打破了語法結構。我重新格式化了代碼,在這里
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;
}
uj5u.com熱心網友回復:
下面的腳本已被修改和修復。
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;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/483831.html
下一篇:蹲下時如何改變速度?
