- Home /
JSON Saving/loading " error CS0029: Cannot implicitly convert type 'void' to 'string' "
Hey Guys! Creeperbot65 here!
I am trying to make a JSON saving/loading script but this error constantly pops up no matter what I do: " Assets/Load.cs(35,22): error CS0029: Cannot implicitly convert type 'void' to 'string' " and I can't seem to fix the problem. Here's the Load.cs code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class Load : MonoBehaviour {
string path;
string jsonString;
public string fileName;
public bool WriteFile;
public string SaveJson;
void Awake() {
LoadSave ();
}
void LoadSave () {
//You can replace "Vars" with your own script name
//You can replace "Cube" with the gameobject you want to get the value from
GameObject SaveGO = GameObject.Find("Cube");
Vars VarScr = SaveGO.GetComponent<Vars>();
if (WriteFile == true) {
///// SAVING SCRIPT \\\\\\
path = Application.streamingAssetsPath + "/" + fileName + ".json";
SaveJson = JsonUtility.ToJson (VarScr);
jsonString = File.WriteAllText (path, SaveJson);
} else if (WriteFile == false) {
///// LOADING SCRIPT \\\\\\
//sets the path to the file in streamingassets
path = Application.streamingAssetsPath + "/" + fileName + ".json";
jsonString = File.ReadAllText (path);
//This gets the values then puts them into a new sorta directory (Like transform.position, just transform is your own thing)
//You can replace "Vars" with your own script name
ObjectData LoadDat = JsonUtility.FromJson<ObjectData> (jsonString);
// Set all vars from this script to the other \\
VarScr.PosX = LoadDat.PosX;
VarScr.PosY = LoadDat.PosY;
VarScr.PosZ = LoadDat.PosZ;
}
}
}
[System.Serializable]
public class ObjectData {
public float PosX;
public float PosY;
public float PosZ;
}
Thanks!
-creeperbot65
Comment
Best Answer
Answer by OneCept-Games · Jan 03, 2018 at 11:20 PM
File.WriteAllText() does not return anything
Oh my goodness, I'm such a noob! Thank you! I should have noticed that earlier!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Generate Json from Class Type List 1 Answer
How to get object array from json? 0 Answers
Distribute terrain in zones 3 Answers
Get JSON array object string value 2 Answers