Unity3d错​​误CS1061:类型x不包含y的定义。但是呢?

时间:2018-07-03 16:37:56

标签: c# unity3d interface

我遇到了前面提到的非常简单的错误。我以为我会很快解决它,但是即使经过一番搜索,我仍然无法一生找出问题所在。我有以下界面:

public interface ITemperatureEmitter
{
    float CurrentTemperatureAddon { get; }
}

我在另外两个(暂时为空)接口中实现了这一点:

public interface ITemperatureEmitterEnvironment : ITemperatureEmitter

public interface ITemperatureEmitterSphere : ITemperatureEmitter

随后,我在下面的类中使用这三个接口:

using System.Collections.Generic;
using UnityEngine;

public class TemperatureReceiver : MonoBehaviour, ITemperatureReceiver
{
    public float PerceivedTemperature;

    // Serialized for debug purposes
    [SerializeField] private List<ITemperatureEmitterSphere> temperatureEmitterSpheres;
    [SerializeField] private List<ITemperatureEmitterEnvironment> temperatureEmitterEnvironments;
    [SerializeField] private float environmentTemperature;
    [SerializeField] private float temperatureToModifyBy;
    [SerializeField] private float currentTemperatureAddon;
    [SerializeField] private float appliedTemperatureAddon;
    [SerializeField] private float totalTemperatureAddon;

    private void Update()
    {
        UpdatePerceivedTemperature();
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.GetComponent<ITemperatureEmitterSphere>() != null)
        {
            temperatureEmitterSpheres.Add(other.GetComponent<ITemperatureEmitterSphere>());
        }
        else if (other.GetComponent<ITemperatureEmitterEnvironment>() != null)
        {
            temperatureEmitterEnvironments.Add(other.GetComponent<ITemperatureEmitterEnvironment>());
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.GetComponent<ITemperatureEmitterSphere>() != null)
        {
            temperatureEmitterSpheres.Remove(other.GetComponent<ITemperatureEmitterSphere>());
        }
        else if (other.GetComponent<ITemperatureEmitterEnvironment>() != null)
        {
            temperatureEmitterEnvironments.Remove(other.GetComponent<ITemperatureEmitterEnvironment>());
        }
    }

    private void UpdatePerceivedTemperature()
    {
        ModifyPerceivedTemperature(temperatureEmitterSpheres);
        ModifyPerceivedTemperature(temperatureEmitterEnvironments);
    }

    private void ModifyPerceivedTemperature<ITemperatureEmitter>(List<ITemperatureEmitter> list)
    {
        if (list.Count > 0)
        {
            foreach (var item in list)
            {
                currentTemperatureAddon += item.CurrentTemperatureAddon;
            }
            currentTemperatureAddon = currentTemperatureAddon / list.Count;
            appliedTemperatureAddon = PerceivedTemperature;
            temperatureToModifyBy = currentTemperatureAddon = appliedTemperatureAddon;
            PerceivedTemperature += temperatureToModifyBy;
        }
    }
}

现在item.CurrentTemperatureAddon中的ModifyPercievedTemperature会发出“ 错误CS1061:类型ITemperatureEmitter不包含CurrentTemperatureAddon的定义,也没有扩展方法{{1可以找到类型为CurrentTemperatureAddon的}}。您是否缺少程序集引用?

ITemperatureEmitter确实包含ITemperatureEmitter的定义...任何人都知道这里发生了什么事吗?

1 个答案:

答案 0 :(得分:3)

您误报了ModifyPerceivedTemperature

private void ModifyPerceivedTemperature<ITemperatureEmitter>(List<ITemperatureEmitter> list)

在这里,您使用ITemperatureEmitter作为通用参数的名称。因此,在此范围内,它不再意味着interface ITemperatureEmitter,而是通用参数。

只需删除通用参数,就不会使用它:

private void ModifyPerceivedTemperature(List<ITemperatureEmitter> list)

现在,您只需使用List<ITemperatureEmitter> list作为参数,您所有的var item in list的类型实际上就是ITemperatureEmitter,而不是通用类型。