[英]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. 我一直在为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."
尝试将我的sprite从接口分配给私有变量时,出现错误“'Sprite'不包含带有0个参数的构造函数”。 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.
没有构造函数为Sprite接受0个参数。 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. 看起来Sprite不包含接受零参数的公共构造函数。
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
妮可·卡里诺纽(Nicole Calinoiu)的回答
The "default" constructor is added by the C# compiler if your class does not contain an explicit instance constructor.
如果您的类不包含显式实例构造函数,则C#编译器将添加“默认”构造函数。 It is a public, parameterless constructor.
它是一个公共的,无参数的构造函数。 https://stackoverflow.com/a/10498709/5569485
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
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 在不了解
Sprite
类的情况下,我的猜测是存在一种用于创建Sprite实例的静态方法
something like 就像是
Sprite sprite = Sprite.Create(...);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.