Search code examples
c#unity-game-enginesaveloadreturn-type

Cannot implicitly convert type 'Coordinate_data' to 'RandomNumber'


I want to save and load data from unity, so i follow how to save object attribute from https://www.youtube.com/watch?v=XOjd_qU2Ido but when i want to return the data to load the data that have saved, the data can't get implicitly.

Here the Coordinate_data Class

using UnityEngine;

[System.Serializable]

public class Coordinate_data
{
    public float[] position;

    public Coordinate_data (RandomNumber coordinate)
    {
        position = new float[3];
        position[0] = coordinate.transform.position.x;
        position[1] = coordinate.transform.position.y;
        position[2] = coordinate.transform.position.z;

    }
}

And here the randomNumber function is to random and show the data that already saved or random

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class RandomNumber : MonoBehaviour
{
    public int MinNum = 0;
    public int MaxNum = 100;

    public Text NilaiRandomX = null;
    public Text NilaiRandomY = null;
    public Text NilaiRandomZ = null;

    public Vector3 position;

    public int StepTime = 2;

    void Start()
    {
        StartCoroutine(RandomNumGenerator());
    }

    IEnumerator RandomNumGenerator()
    {
        while (1 != 0)
        {
            yield return new WaitForSeconds(StepTime);
            int X = UnityEngine.Random.Range(MinNum, MaxNum);
            int Y = UnityEngine.Random.Range(MinNum, MaxNum);
            int Z = UnityEngine.Random.Range(MinNum, MaxNum);

            float nilaix = X;
            float nilaiy = Y;
            float nilaiz = Z;

            position = new Vector3(nilaix, nilaiy, nilaiz);
            transform.position = position;

            //NilaiRandomX.text = + X;
            NilaiRandomX.GetComponent<Text>().text = "" + X;
            //NilaiRandomY.text = Y;
            NilaiRandomY.GetComponent<Text>().text = "" + Y;
            //NilaiRandomZ.text = Z;
            NilaiRandomZ.GetComponent<Text>().text = "" + Z;

        }
    }
}

Here the code that to save and load data

using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

public static class SaveSystem
{
    public static void saveCoordinate (RandomNumber coordinate)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        string path = Application.persistentDataPath + "coordinate.bin";
        FileStream stream = new FileStream(path, FileMode.Create);

        Coordinate_data data = new Coordinate_data(coordinate);

        formatter.Serialize(stream, data);

        stream.Close();
    }

    public static RandomNumber LoadPlayer()
    {
        string path = Application.persistentDataPath + "coordinate.bin";
        if (File.Exists(path))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream(path, FileMode.Open);
            Coordinate_data data = formatter.Deserialize(stream) as Coordinate_data;
            stream.Close();

            return data; // fail to return data in here
        }
        else
        {
            Debug.LogError("Save File Not Found in " + path);
            return null;
        }
    }
}

Everything works fine except that I now get this error message on the "return person" in the if statement.

Cannot implicitly convert type 'Coordinate_data' to 'RandomNumber'

Can anyone please help? Thanks in advance.


Solution

  • Because you have declared function to return RandomNumber!

    In this line:

    public static RandomNumber LoadPlayer() // This function is supposed to return RandomNumber
    {
       ...
       Coordinate_data data = formatter.Deserialize(stream) as Coordinate_data;
       return data; // And returning Coordinate_data.
    }
    

    It seems like you are new to C# or Unity, so here is pro tip: Believe what compiler is saying. When the compiler says Cannot implicitly convert type 'Coordinate_data' to 'RandomNumber', then there must be an error that you are using Coordinate_data type where supposed to be using RandomNumber.

    I'm not sure that what's the desired action, but if you want to return Coordinate_data, then just change it's return type, like:

    public static Coordinate_data LoadPlayer()
    

    BTW, Implicit type conversion means, when something's type mismatches, compiler tries to interpret it with given type, like:

    private void DoSomthingWithFloat(float x)
    {
        ...
    }
    
    DoSomethingWithFloat(1);
    

    In above example, parameter x is supposed to be float, but it's called with int value. so compiler implicitly casts it to float.

    But in your case, there is no way to cast Coordinate_data to RandomNumber, so compiler has thrown error.