程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了我收到了 NullReferenceException 错误,但没有成功!有人可以帮我解决这种情况吗?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决我收到了 NullReferenceException 错误,但没有成功!有人可以帮我解决这种情况吗??

开发过程中遇到我收到了 NullReferenceException 错误,但没有成功!有人可以帮我解决这种情况吗?的问题如何解决?下面主要结合日常开发的经验,给出你关于我收到了 NullReferenceException 错误,但没有成功!有人可以帮我解决这种情况吗?的解决方法建议,希望对你解决我收到了 NullReferenceException 错误,但没有成功!有人可以帮我解决这种情况吗?有所启发或帮助;

嘿伙计们,我在我的统一控制台中收到此错误 我想做一个2d剑斗游戏。 但我无法伤害敌人。 请帮忙!

NullReferenceException: Object reference not set to an instance of an object
Dodamage.dodamage () (at Assets/Scripts/Dodamage.cs:16)
PlayerAttack.SwordCombat () (at Assets/Scripts/PlayerAttack.cs:54)
PlayerAttack.Update () (at Assets/Scripts/PlayerAttack.cs:25)

我的 PlayerAttack 脚本 此脚本附加到播放器 这个脚本中的所有公共游戏对象都被填满

public class PlayerAttack : MonoBehavIoUr {

    public Animator anim;
    private Vector2 movement;
    private Vector2 sprint;
    private float doubleClickTime = 0.2f;
    private float lastClickTime;
    [SerializefIEld]
    public bool punched;
    public bool isArmed = false;
    private GameObject sword;

    voID Start(){
        movement = gameObject.GetComponent<PlayerMove>().movement;
        sprint = gameObject.GetComponent<PlayerMove>().sprinting;
    }

    voID Update () {
        if(isArmed){
            sword = gameObject.GetComponent<PickupWeapon>().sword;
            SwordCombat();
        }else{
            HandCombat();
        }
    }

    public voID HandCombat(){
        if(input.GetKeyDown(KeyCode.Mouse0)){
            float timeSinceLastClick = Time.time - lastClickTime;
            if(timeSinceLastClick < doubleClickTime && punched){
                anim.SetTrigger("DoublePunch");
                punched = false;
            }else{
                anim.SetTrigger("Punch 1");
                punched = true;
            }
            lastClickTime = Time.time;
        }
    }

    public voID SwordCombat(){
        if(input.GetKeyDown(KeyCode.Mouse0)){
            float timeSinceLastClick = Time.time - lastClickTime;
            if(timeSinceLastClick < doubleClickTime && punched){
                anim.SetTrigger("DoubleSwordAttack");
                sword.GetComponent<Dodamage>().dodamage();
                punched = false;
            }else{
                anim.SetTrigger("SwordAttack");
                sword.GetComponent<Dodamage>().dodamage();
                punched = true;
            }
            lastClickTime = Time.time;
        }
    }
}

我的 Dodamage 脚本

这个脚本负责伤害敌人

public class Dodamage : MonoBehavIoUr {

    public transform attackPoint;
    public float attackRange = 0.5f;
    public LayerMask enemyLayers;
    private GameObject damageTaker;

    public voID dodamage(){
        CollIDer2D[] hitEnemIEs = Physics2D.OverlapCircleAll(attackPoint.position,attackRange,enemyLayers);

        foreach(CollIDer2D enemy in hitEnemIEs){
            enemy.gameObject.GetComponent<Control>().Takedamage(50f);
        }
    }

    voID OnDrawGizmosSelected(){
        if(attackPoint == null){
            return;
        }
        Gizmos.DrawWireSphere(attackPoint.position,attackRange);
    }

}

我的控制脚本

此脚本附加到敌人并包含其主要功能

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

public class Control : MonoBehavIoUr {

    public float speed = 8f;
    public float maxHealth = 100;
    float currentHealth;

    voID Start(){
        currentHealth = maxHealth;
    }

    public voID Takedamage(float damage){
        currentHealth -= damage;
        if(currentHealth <= 0){
            DIE();
        }
    }

    public voID DIE(){
        DeBUG.Log("Enemy DIEd!");
    }

}

我尝试了多种解决方案。 我已经使用关于 2D 战斗系统的 brackeys 教程之一编写了此代码。 它与我的代码相同,但出现错误。

我不知道错误是什么意思,也没有得到完美的解决方案。 请帮忙!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的我收到了 NullReferenceException 错误,但没有成功!有人可以帮我解决这种情况吗?全部内容,希望文章能够帮你解决我收到了 NullReferenceException 错误,但没有成功!有人可以帮我解决这种情况吗?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: