简体   繁体   中英

Unity C# Error: 'Sprite' does not contain a constructor that takes 0 arguments

I've been working on an item system for my game in Unity. I am still pretty new to coding, but I am giving it my best effort.

My Item system Works by accessing interfaces with the data I need. While trying to assign my sprite from the interface to a private variable, I get the error "'Sprite' does not contain a constructor that takes 0 arguments." I have looked all over for solutions, and haven't found any fixes that have worked for me so far.

The Class I created to access the interface looks like this:

public class ISType : IISType {

    [SerializeField] string _name;

    [SerializeField] Sprite _icon;

    ISType()
    {
        _name = "Type";
        _icon = new Sprite(); }

    public string Name
    {
        get
        { return _name; }

        set
        { _name = value }
    }

    public Sprite Icon {
        get
        { return _icon; }

        set
        { _icon = value; }
}

}

If anyone can tell what is going on I would really appreciate the help! :)

The answer is in the error. There is no constructor that takes 0 parameters for Sprite. Without seeing the code I'm guessing you made a custom constructor with parameters and didn't add a paramaterless one.

A default parameterless constructor would look like:

Sprite()
{}

Be sure to do a lot more reading and tutorials. This is fairly basic class information.

It looks like Sprite does not contain a public constructor accepting zero arguments.

A class with no constructors defined will have a parameterless constructor.

public class MyClass { }

MyClass x= new MyClass(); // this is valid

However if it has any other constructors defined, this parameterless 'default' constructor is no longer 'a given'.

Difference between default constructor and paramterless constructor?

Answer by Nicole Calinoiu

The "default" constructor is added by the C# compiler if your class does not contain an explicit instance constructor. It is a public, parameterless constructor. https://stackoverflow.com/a/10498709/5569485

public class MyClass { 
    public MyClass(string foo)
    { 
    }
}

MyClass x= new MyClass(); // this is invalid 

The class would have to manually define a parameterless constructor.

public class MyClass { 
   // parameterless constructor
    public MyClass()
    { 
    }

    public MyClass(string foo)
    { 
    }
}

MyClass x= new MyClass(); // this is valid again!

Sometimes no constructors are provided publicly, and a class instead provides static methods to instantiate the object.

public class MyClass
{
    private MyClass()
    {
    }

    public static MyClass Create()
    {
        return new MyClass();
    }
}

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/private-constructors

A private constructor is a special instance constructor. It is generally used in classes that contain static members only. If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class.

Without knowing more about the Sprite class, my guess is that there is a static method for creating instances of the Sprite

something like

Sprite sprite = Sprite.Create(...);

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