Reputation: 11
Can anybody explain whats the Error ?
IEnumerator GetPictureCor(string url, FriendData friend)
Sprite sprite = new Sprite();
WWW www = new WWW(url);
yield return www;
sprite = Sprite.Create(www.texture, new Rect(0, 0, 128, 128), new Vector2(0, 0), 1f);
friend.picture = sprite;
// print ("get picture for " + url);
}
The Error is :
Assets\ADogsAdventure\Scripts\Integrations\FacebookManager.cs(419,29): error CS1729: 'Sprite' does not contain a constructor that takes 0 arguments
Upvotes: 1
Views: 63
Reputation: 5173
As the error states there is no constructor for Sprite
that takes 0 argument, this is because Sprite
is not intended to be used with Sprite sprite = new Sprite()
but instead has a method named Sprite.Create();
that should be used for the creation of a sprite.
As per the Unity docs for Sprite.Create()
Sprite.Create creates a new Sprite which can be used in game applications. A texture needs to be loaded and assigned to Create in order to control how the new Sprite will look.
public class spriteCreate : MonoBehaviour
{
public Texture2D tex;
private Sprite mySprite;
private SpriteRenderer sr;
void Awake()
{
sr = gameObject.AddComponent<SpriteRenderer>() as SpriteRenderer;
sr.color = new Color(0.9f, 0.9f, 0.9f, 1.0f);
transform.position = new Vector3(1.5f, 1.5f, 0.0f);
}
void Start()
{
mySprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f);
}
void OnGUI()
{
if (GUI.Button(new Rect(10, 10, 100, 30), "Add sprite"))
{
sr.sprite = mySprite;
}
}
}
More information about error CS1729 can be found in the C# documentation, where it explains the the give constructor has no implementation for the amount of parameters you are passing (in this case 0, or any other amount of parameters, since there is no public constructor for Sprite
)
From the above linked microsoft docs:
This error occurs when you either directly or indirectly invoke the constructor of a class but the compiler cannot find any constructors with the same number of parameters. In the following example, the test class has no constructors that take any arguments. It therefore has only a parameterless constructor that takes zero arguments. Because in the second line in which the error is generated, the derived class declares no constructors of its own, the compiler provides a parameterless constructor. That constructor invokes a parameterless constructor in the base class. Because the base class has no such constructor, CS1729 is generated.
Upvotes: 2