Cannot convert 'X' to 'X'

I have no idea how to fix this error, I am absolutely lost, I would greatly appreciate any help.
The error is Argument 2: cannot convert ‘WordDisplayYear1’ to ‘WordDisplay’. The error is on line 3 of the code below, which is in the WordManagerYear1Script:

 public void AddWord()
        {
            WordYear1 wordYear1 = new Word(WordGeneratorYear1.GetRandomWord(), wordSpawnerYear1.SpawnWordYear1());
            Debug.Log(wordYear1.toString());
            words.Add(wordYear1);
        }

I believe the code below in the WordSpawnerYear1 script is also connected to this problem:

    public class WordSpawnerYear1 : MonoBehaviour
    {
        public GameObject wordPrefab;
        public Transform wordCanvas;
 
        public WordDisplayYear1 SpawnWordYear1()
        {
 
            GameObject wordObj = Instantiate(wordPrefab, wordCanvas);
            WordDisplayYear1 wordDisplayYear1 = wordObj.GetComponent<WordDisplayYear1>();
 
            return wordDisplayYear1;
        }
    }

Tell me if you need to see more code, thankyou in advance. :slight_smile:

The problem is in your Word constructor. It’s expecting an argument of type WordDisplay and you’re passing type WordDisplayYear1. It’s hard to tell what all the types are without more code.

1 Like

I appreciate the quick reply!
here is the wordYear1 script:

public class WordYear1 : MonoBehaviour
{
    public string word;
    private int typeIndex;

    WordDisplayYear1 displayYear1;

    public WordYear1(string _word, WordDisplayYear1 _displayYear1)
    {
        word = _word;
        typeIndex = 0;

        displayYear1 = _displayYear1;
        displayYear1.SetWord(word);
    }

    public char GetNextLetter()
    {
        return word[typeIndex];
    }

    public void TypeLetter()
    {
        typeIndex++;
        displayYear1.RemoveLetter();
    }

    public bool WordTyped()
    {
        bool wordTyped = (typeIndex >= word.Length);
        if (wordTyped)
        {
            displayYear1.RemoveWord();
        }
        return wordTyped;
    }

    public string toString()
    {
        return word;
    }
}

wordManagerYear1 script:

public class WordManagerYear1 : MonoBehaviour
{
    public List<WordYear1> words;

    public WordSpawner wordSpawnerYear1;

    public bool hasActiveWord;
    public WordYear1 activeWord;

    public int listLength;

    public void Update()
    {
        listLength = words.Count;
    }

    //removes the word on screen
    public void rAW()
    {
        words.Remove(words[Random.Range(0, words.Count)]);

        //this removes what is stored in activeWord from the list
        //   words.Remove(activeWord);
    }


    public void AddWord()
    {
        WordYear1 wordYear1 = new Word(WordGeneratorYear1.GetRandomWord(), wordSpawner.SpawnWordYear1());
        Debug.Log(wordYear1.toString());
        words.Add(wordYear1);
    }

    public void hAWF()
    {
        hasActiveWord = false;
    }

    public void hAWT()
    {
        hasActiveWord = true;
    }

    public void TypeLetter(char letter)
    {
        if (hasActiveWord)
        {
            if (activeWord.GetNextLetter() == letter)
            {
                activeWord.TypeLetter();
            }
        }
        else
        {
            foreach (WordYear1 wordYear1 in words)
            {
                if (wordYear1.GetNextLetter() == letter)
                {
                    activeWord = wordYear1;
                    hasActiveWord = true;
                    wordYear1.TypeLetter();
                    break;
                }
            }
        }

        if (hasActiveWord && activeWord.WordTyped())
        {
            hasActiveWord = false;
            words.Remove(activeWord);
        }
    }
}

WordSpawnerYear1 script:

public class WordSpawnerYear1 : MonoBehaviour
{
    public GameObject wordPrefab;
    public Transform wordCanvas;

    public WordDisplayYear1 SpawnWordYear1()
    {

        GameObject wordObj = Instantiate(wordPrefab, wordCanvas);
        WordDisplayYear1 wordDisplayYear1 = wordObj.GetComponent<WordDisplayYear1>();

        return wordDisplayYear1;
    }
}

WordDisplayYear1 script:

public class WordDisplayYear1 : MonoBehaviour
{
    public Text text;

    public void Update()
    {
        //allows us to gain access to variables and methods in the WordTimer script
        GameObject WordManagerYear1 = GameObject.Find("WordManager");
        WordTimerYear1 wordTimerYear1 = WordManagerYear1.GetComponent<WordTimerYear1>();

        //allows us to gain access to variables and methods in the WordManager script
        WordManagerYear1 wordManagerYear1 = WordManagerYear1.GetComponent<WordManagerYear1>();

        if (wordTimerYear1.onScreenTimer >= 150)
        {
            //allows us to gain access to variables and methods in the player script
            GameObject Player = GameObject.Find("Player");
            PlayerController playerController = Player.GetComponent<PlayerController>();

            //minuses one from the players health
            playerController.health = playerController.health - 1;


            //removes this object from the list when then word has atleast one letter typed
            wordManagerYear1.rAW();

            //destroys this object
            Destroy(gameObject);

            //is hAW is true, the it sets hAW to flase
            if (wordManagerYear1.hasActiveWord == true)
            {
                wordManagerYear1.hAWF();
            }

            //sets variable to false
            wordTimerYear1.isWordActive = false;

            //sets the players on screen timer to 0
            wordTimerYear1.onScreenTimer = 0;

        }

    }

    //where the onscreen word gets set
    public void SetWord(string wordYear1)
    {
        text.text = wordYear1;
    }

    //removes a letter from the word when it is typed by the player
    //also makes the other letters red
    public void RemoveLetter()
    {
        text.text = text.text.Remove(0, 1);
        text.color = Color.red;
    }

    //removes the word from the screen
    public void RemoveWord()
    {
        Destroy(gameObject);
    }
}

tell me if you still need to see more. Also is there a way to be able to pass it WordDisplayYear1 instead of WordDisplay?

Honestly I’m not sure what your logic is trying to do. The problem is in your Word class, which you haven’t posted. It has a constructor that looks something like this I’m guessing:

class Word
{
    Word(string, WordDisplay) { }
}

But then this code, which I now understand to be a MonoBehaviour and not a plain C# class:

WordYear1 wordYear1 = new Word(WordGeneratorYear1.GetRandomWord(), wordSpawner.SpawnWordYear1());

Is trying to create a MonoBehaviour with a new statement, which isn’t how Unity works. You need to Instantiate a clone from a prefab. You seem to be mixing paradigms and getting confused somewhere in the middle.

3 Likes

Generally speaking, some help to fix “Cannot implicitly convert type ‘Xxxxx’ into ‘Yyyy’:”

http://plbm.com/?p=263