【问题标题】:How do I solve CS7036 error between classes in unity?如何统一解决类之间的 CS7036 错误?
【发布时间】:2019-12-04 18:42:55
【问题描述】:

我假设这两个代码块涉及以下错误:

Assets\Scripts\Weapons.cs(8,12): 错误 CS7036: 没有给出与 'Item.Item(string, int)' 的所需形参 'name' 对应的参数

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

public class Item : MonoBehaviour
{
    private string name;
    private int quantity;

    public Item(string name, int quantity)
    {
        this.name = name;
        this.quantity = quantity;
    }
    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public int Quantity
    {
        get { return quantity; }
        set { quantity = value; }
    }

    public virtual void UseItem()
    {

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

public class Weapons : Item
{
    public GameObject bullet;
    public GameObject shotgunSpawn;
    public GameObject shotgunSpawn2;

    //public bool useGun;

    public Inventory iw;
    GUI_2D m;

    public float Speed;

    GameObject patroller;
    GameObject guard;

    public bool pellet;
    public bool shotGun;
    public bool barGun;
    public PlayerController player;

    // Start is called before the first frame update
    void Start()
    {
        Speed = 5f;
        player = GameObject.FindGameObjectWithTag("Player");
        patroller = GameObject.FindGameObjectWithTag("Patroller");
        guard = GameObject.FindGameObjectWithTag("Guard");
        guard = GameObject.FindGameObjectWithTag("Shotgun");
        pellet = false;
        shotGun = false;
        barGun = false;
    }

    void DestroyEnemy()
    {
        if (patroller)
        {
            patroller.SetActive(false);
        }
        if (guard)
        {
            patroller.SetActive(false);
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (iw.invItems.Count < 12)
        {
            if (other.gameObject.CompareTag("Player"))
            {
                pellet = true;
            }
        }
    }

    public override void UseItem()
    {
        if (pellet)
        {
            player.pellet = true;
            player.shotGun = false;
            player.barGun = false;
        }
        if (shotGun)
        {
            player.pellet = false;
            player.shotGun = true;
            player.barGun = false;
        }
        if (barGun)
        {
            player.pellet = false;
            player.shotGun = false;
            player.barGun = true;
        }
         base.UseItem();
    }
}

我不想更改项目类,因为这样做会影响另一个依赖于项目类构造函数的类。除非有另一种方法可以通过更改项目类别来解决错误。给出的错误也以同样的方式影响另一个类。我希望从这里的答案中解决武器类和其他类的问题。提前谢谢你。

【问题讨论】:

    标签: c# visual-studio unity3d


    【解决方案1】:

    shouldn't add constructors 给 MonoBehaviours,因为 Unity 引擎负责实例化它们并且永远不会传递你的构造函数参数(你也不应该创建 MonoBehaviours 的实例)。

    只需删除您的 Item 构造函数并在需要时手动分配值。其他构造代码在Awake()Start()完成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-19
      • 1970-01-01
      • 2018-01-28
      • 2019-01-30
      • 2013-11-22
      • 1970-01-01
      • 2019-03-22
      • 2012-04-07
      相关资源
      最近更新 更多