简体   繁体   English

如何统一解决类之间的 CS7036 错误?

[英]How do I solve CS7036 error between classes in unity?

These two blocks of code I assume are involved with the following error:我假设这两个代码块与以下错误有关:

Assets\\Scripts\\Weapons.cs(8,12): error CS7036: There is no argument given that corresponds to the required formal parameter 'name' of 'Item.Item(string, int)' 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();
    }
}

I am not trying to change the item class since doing so will affect another class that depends on the item classes constructor.我不想更改项目类,因为这样做会影响另一个依赖于项目类构造函数的类。 Unless there is another way to resolve the error by changing the item class.除非有另一种方法可以通过更改项目类来解决错误。 The error given is also affecting another class in the same way.给出的错误也以同样的方式影响另一个类。 I am hoping to solve the issue in both the weapon class and the other class from an answer here.我希望从这里的答案中解决武器类和其他类中的问题。 Thank you in advance.先感谢您。

You shouldn't add constructors to MonoBehaviours, as the Unity Engine is responsible for instantiating them and will never pass your constructors arguments (you shouldn't be creating instances of MonoBehaviours, either).不应该向 MonoBehaviours 添加构造函数,因为 Unity Engine 负责实例化它们并且永远不会传递你的构造函数参数(你也不应该创建 MonoBehaviours 的实例)。

Just remove your Item constructor and assign the values manually whenever needed.只需删除您的 Item 构造函数并在需要时手动分配值。 Other construction code should be done in Awake() or Start() .其他构造代码应该在Awake()Start()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM