Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实例化后类实例为null_C#_Unity3d - Fatal编程技术网

C# 实例化后类实例为null

C# 实例化后类实例为null,c#,unity3d,C#,Unity3d,更新:如以下评论所述,目标为目标公司经理;(在MakeAbility中)为空。这似乎是问题的根源,但为什么它是空的 =========== 在下面的代码中,我创建了PartAttack类的一个实例,然后将其指定为我的目标管理器的引用 第一个调试日志返回PartAttack。第二个返回null 公共目标管理器(在第一个脚本中)在inspector中分配给一个带有目标脚本的游戏对象 我做错了什么?为什么不能正确分配目标管理器 using System.Collections; usi

更新:如以下评论所述,目标为目标公司经理;(在MakeAbility中)为空。这似乎是问题的根源,但为什么它是空的

===========

在下面的代码中,我创建了PartAttack类的一个实例,然后将其指定为我的目标管理器的引用

第一个调试日志返回PartAttack。第二个返回null


公共目标管理器(在第一个脚本中)在inspector中分配给一个带有目标脚本的游戏对象

我做错了什么?为什么不能正确分配目标管理器

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

public class MakeAbility : MonoBehaviour
{
    public BlockScriptableObject block;

    public IDictionary<string, IAbility> abilities_parts = new Dictionary<string, IAbility>();


    public Targeting target_manager;
    public PartAttack part_attack = new PartAttack();

    private void Start()
    {
        part_attack.block_attack_damage = block.attack_damage;
        part_attack.target_manager = target_manager;
        Debug.Log(part_attack);
        Debug.Log(part_attack.target_manager);

        abilities_parts.Add("part_attack", part_attack);

    }

}

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


[System.Serializable]
public class PartAttack: IAbility
{
    public Targeting target_manager;

    public void Use()
    {

    }

}


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

public class Targeting : MonoBehaviour
{
    public int max_targets = 3;
    public string current_target = null;
    public List<Combatant> list_of_targetable = new List <Combatant>();

    public BlockScriptableObject block; // The abilityblock

    public GameObject target_clickfield_char1;
    public Combatant character_slot_1;

}

使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用制度;
公共类可制造性:单一行为
{
公共块ScriptableObject块;
公共IDictionary能力_parts=new Dictionary();

公共目标管理者; public PartAttack part_attack=新PartAttack(); 私有void Start() { 部分攻击。封锁攻击伤害=封锁攻击伤害; part_attack.target_manager=target_manager; 调试日志(part_攻击); Debug.Log(part\u attack.target\u manager); 能力部分。添加(“部分攻击”,部分攻击); } } 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; [系统可序列化] 公共类攻击:可参与性 {
公共目标管理者; 公共用途() { } } 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 公共类目标:单一行为 { 公共int max_targets=3; 公共字符串current_target=null; public List List of_targetable=新列表(); public BlockScriptableObject block;//能力块 公共游戏对象目标\单击字段\字符1; 公共战斗人员角色1; }
TargetManager似乎没有在任何地方实例化

您需要将TargetManager设置为内存分配。这是通过实例化完成的

您可以为聚合类创建构造函数,通过实例化变量来分配此内存,也可以将target_manager设置为main函数中的某个新内存分配

public Targeting target_manager = new Targeting();
C#很快。而且我们不能期望在声明指针后这么快就看到实例化的对象。我知道在系统的其他地方有一个游戏对象最终会分配它,但是对于你的调试脚本来说,这还没有发生

如果您希望始终在构造函数中包含某些内容,则需要将其分配给构造函数中的空对象。特别是如果您编写了一行代码,在声明其存在后将立即访问它。不要依赖存在于不同对象中的其他代码。对于计算机来说,代码是非常遥远的

这不会影响你以后的作业,因为重新分配不会破坏任何东西,它只会在其他地方分配更多内存


如果您担心内存泄漏,请在重新声明之前处理该对象。

公共目标目标管理器在检查器中分配给带有目标脚本的游戏对象。
公共目标管理器(在MakeAbility中)和
公共目标管理器(在PartAttack中)不是同一个对象。这最初是正确的,但我是否将PartAttack目标\u管理器设置为与另一个相同的实例?是
公共目标\u管理器(在MakeAbility中)非空?您不能使用new或构造函数创建MonoBehavior类型的实例。创建新实例的唯一方法是使用gameObjectReference.AddComponent()将其添加到现有游戏对象中!你不是说已经有相应的游戏对象在检查器中引用了吗?为什么要使用
public Targeting_manager=new Targeting()(这是不允许的)那么?添加您提到的特定行不幸没有改变任何结果。我在每节课上单独添加了它,也在两个课上一起添加了它。第一个调试仍然是PartAttack,第二个调试仍然是null。有趣的答案,很容易理解,也很有趣。正因为如此,我想说,我们需要更多的代码来帮助。我现在正在添加目标类。出现了一个想法,这可能是由于不从MonoBehavior派生时的不同实例化规则造成的吗?您不能使用
new
或构造函数创建类型为
MonoBehavior
的实例。创建新实例的唯一方法是使用
gameObjectReference.AddComponent()
将其添加到现有的游戏对象中!