Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the file path variable for saving data.
Unity
string filePath = Application.persistentDataPath + [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the slash '/' before the file name.
✗ Incorrect
The file path must include a slash before the file name to form a valid path.
2fill in blank
mediumComplete the code to write a JSON string to a file.
Unity
File.WriteAllText([1], jsonData); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the data string instead of the file path.
✗ Incorrect
The first argument to WriteAllText is the file path where data will be saved.
3fill in blank
hardFix the error in reading the file content into a string.
Unity
string jsonData = File.[1](filePath); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ReadAllLines which returns a string array instead of a string.
✗ Incorrect
Use ReadAllText to read the entire file content as a single string.
4fill in blank
hardFill both blanks to deserialize JSON data into a PlayerData object.
Unity
PlayerData player = JsonUtility.[1]<[2]>(jsonData);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ToJson which converts object to JSON string instead of the opposite.
✗ Incorrect
Use FromJson to convert JSON string to an object of type PlayerData.
5fill in blank
hardFill all three blanks to save player score to a JSON file.
Unity
string jsonData = JsonUtility.[1](playerData); File.[2]([3], jsonData);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up ToJson and FromJson methods.
Using ReadAllText instead of WriteAllText for saving.
✗ Incorrect
First convert the playerData object to JSON string using ToJson, then write it to the file path using WriteAllText.