0

As I tried to make a word scrambled game, it gave me error

Assets\Scripts\WordScramble\WordScramble.cs(119,26): error CS0266: Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?)

Assets\Scripts\WordScramble\WordScramble.cs(269,34): warning CS0642: Possible mistaken empty statement

What could possibly goes wrong? It a little messy as this was a tryout but please help!

using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Globalization;
using System.Reflection;

namespace GameCoreSystem
{
    [System.Serializable]
    public class Result
    {
        public int totalScore = 0;

        [Header("REF UI")]
        public Text textTime;
        public Text textTotalScore;

        [Header("REF RESET SCREEN")]
        public Text textResultScore;
        public Text textInfo;
        public GameObject resultCanvas;

        public void ShowResult ()
        {
            //show damage dealt
            textResultScore.text = totalScore.ToString();
            textInfo.text = "Your damage is " + WordScramble.main.words.Length + " damages";

            int allTimeLimit = WordScramble.main.GetAllTimeLimit();

            resultCanvas.SetActive(true);
        }
    }

    [System.Serializable]
    public class Word
    {
        public string word;
        [Header("leave empty if you want to randomized")]
        public string desiredRandom;

        [Space(10)]
        public int timeLimit;



        public string GetString()
        {
            if (!string.IsNullOrEmpty(desiredRandom))
            {
                return desiredRandom;
            }

            string result = word;

            while (result == word)
            {
                result = "";
                List<char> characters = new List<char>(word.ToCharArray());
                while (characters.Count > 0)
                {
                    int indexChar = Random.Range(0, characters.Count - 1);
                    result += characters[indexChar];

                    characters.RemoveAt(indexChar);
                }
            }

            return result;
        }
    }



    public class WordScramble : MonoBehaviour
    {
        public Word[] words;

        [Space(10)]
        public Result result;


        [Header("UI REFERENCE")]
        public GameObject wordCanvas;
        public CharObject prefab;
        public Transform container;
        public float space;
        public float lerpSpeed = 5;

        List<CharObject> charObjects = new List<CharObject>();
        CharObject firstSelected;

        public int currentWord;

        public static WordScramble main;

        public int totalScore;

        void Awake()
        {
            main = this;
        }

        // Start is called before the first frame update
        void Start()
        {
            ShowScramble(currentWord);
            result.textTotalScore.text = result.totalScore.ToString();
        }

        // Update is called once per frame
        void Update()
        {
            RepositionObject();

            //Update Score and calculate
            totalScore = Mathf.Lerp(totalScore, result.totalScore, Time.deltaTime * 5);
            result.textTotalScore.text = Mathf.RoundToInt(totalScore).ToString();

        }

        public int GetAllTimeLimit ()
        {
            float result = 0;
            foreach (Word w in words)
            {
                result += w.timeLimit / 2;
            }

            return Mathf.RoundToInt(result);
        }

        void RepositionObject()
        {
            if (charObjects.Count == 0)
            {
                return;
            }

            float center = (charObjects.Count - 1) / 2;
            for (int i = 0; i < charObjects.Count; i++)
            {
                charObjects[i].rectTransform.anchoredPosition
                    = Vector2.Lerp(charObjects[i].rectTransform.anchoredPosition,
                    new Vector2((i - center) * space, 0), lerpSpeed * Time.deltaTime);
                charObjects[i].index = i;
            }
        }

        public void ShowScramble()
        {
            ShowScramble(Random.Range(0, words.Length - 1));
        }

        public void ShowScramble(int index)
        {
            charObjects.Clear();
            foreach (Transform child in container)
            {
                Destroy(child.gameObject);
            }

            //FINISHED DEBUG
            if (index > words.Length - 1)
            {
                result.ShowResult();
                wordCanvas.SetActive(false);
                //Debug.LogError("index out of range, please enter number between 0-" + (words.Length - 1).ToString());
                return;
            }

            char[] chars = words[index].GetString().ToCharArray();
            foreach (char c in chars)
            {
                CharObject clone = Instantiate(prefab.gameObject).GetComponent<CharObject>();
                clone.transform.SetParent(container);
                clone.Init(c);

                charObjects.Add(clone);

            }

            currentWord = index;
            StartCoroutine(TimeLimit());
        }

        public void Swap(int indexA, int indexB)
        {
            CharObject tmpA = charObjects[indexA];

            charObjects[indexA] = charObjects[indexB];
            charObjects[indexB] = tmpA;

            charObjects[indexA].transform.SetAsLastSibling();
            charObjects[indexB].transform.SetAsLastSibling();

            CheckWord();
        }

        public void Select(CharObject charObject)
        {
            if (firstSelected)
            {
                Swap(firstSelected.index, charObject.index);

                //Unselect
                firstSelected.Select();
                charObject.Select();

            }
            else
            {
                firstSelected = charObject;
            }
        }

        public void UnSelect()
        {
            firstSelected = null;
        }

        public void CheckWord()
        {
            StartCoroutine(CoCheckWord());
        }

        //Check if right or wrong
        IEnumerator CoCheckWord ()
        {
            yield return new WaitForSeconds(0.5f);

            string word = "";
            foreach (CharObject charObject in charObjects)
            {
                word += charObject.character;
            }

            if (timeLimit <= 0)
            {
                currentWord++;
                ShowScramble(currentWord);
                yield break;
            }

            if (word == words[currentWord].word)
            {
                currentWord++;
                result.totalScore += Mathf.RoundToInt(timeLimit);

                //StopCorontine(TimeLimit());

                ShowScramble(currentWord);
            }
        }

        //Change time limit here
        public int timeLimit;
        IEnumerator TimeLimit ()
        {
            float timeLimit = words[currentWord].timeLimit;
            result.textTime.text = Mathf.RoundToInt(timeLimit).ToString();

            int myWord = currentWord;

            yield return new WaitForSeconds(1);

            while (timeLimit > 0);
            {
                if (myWord != currentWord) { yield break; }

                timeLimit -= Time.deltaTime;
                result.textTime.text = Mathf.RoundToInt(timeLimit).ToString();
                yield return null;
            }
            //score text
            CheckWord();
        }
    }
}

Assets\Scripts\WordScramble\WordScramble.cs(119,26): error CS0266: Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?)

Assets\Scripts\WordScramble\WordScramble.cs(269,34): warning CS0642: Possible mistaken empty statement

Nirl
  • 3
  • 3
  • The compiler is trying to help you out. If you could convert `float v = 3.4f` to `int i = v;` then you'd get `int i = 3;`. You have to explicitly opt in to this behaviour by writing `int i = (float)v;` – ProgrammingLlama Nov 04 '20 at 08:31
  • The second is for this line: `while (timeLimit > 0);`. Do you note the trailing ';'? – Klaus Gütter Nov 04 '20 at 08:33
  • Also, another thing I noticed while looking at your all `GetAllTimeLimit()` method is that you might be using integer division when you don't want to (if `w.timeLimit` is `int`). If that's the case, see [this question](https://stackoverflow.com/questions/9288904/division-returns-zero). – ProgrammingLlama Nov 04 '20 at 08:34
  • P.S. If you're still stuck: `Mathf.Lerp` returns a `float` but `totalScore` is an `int`. – ProgrammingLlama Nov 04 '20 at 08:37
  • I've got it, thank you! – Nirl Nov 04 '20 at 09:53

0 Answers0