Home > Enterprise >  Sprite does not contain a definition for sprite and no accessible extension method 'sprite'
Sprite does not contain a definition for sprite and no accessible extension method 'sprite'

Time:07-23

im using unity and i got this error:

enter image description here

and the code is:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

public class CharacterManager : MonoBehaviour
{
    public CharacterDatabase characterDB;

    public Text nameText;
    public Sprite artworkSprite;

    private int selectedOption = 0;
    // Start is called before the first frame update
    void Start()
    {
        UpdateCharacter(selectedOption);
    }

    public void NextOption()
    {
        selectedOption  ;

        if(selectedOption >= characterDB.CharacterCount)
        {
            selectedOption = 0;
        }

        UpdateCharacter(selectedOption);
    }

    public void BackOption()
    {
        selectedOption--;

        if(selectedOption < 0)
        {
            selectedOption = characterDB.CharacterCount - 1;
        }

        UpdateCharacter(selectedOption);
    }

    private void UpdateCharacter(int selectedOption)
    {
        Character character = characterDB.GetCharacter(selectedOption);
        artworkSprite.sprite = character.characterSprite; <-- where the error is
        nameText.text = character.characterName;
    }
}

CodePudding user response:

you can use 2 ways to set sprite

 artworkSprite = character.characterSprite;

or

public Image artworkImage;
...
artworkImage.sprite = character.characterSprite;

I hope it will work on your project.

  • Related