简体   繁体   中英

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)'

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).

Just remove your Item constructor and assign the values manually whenever needed. Other construction code should be done in Awake() or Start() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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