How do I write to Json file in C#? [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am making a game in Unity. I was trying to make an Options menu, but my apply button does not work. I wanted to make it so the options gets written to a Json file.
I have tried that, but I just get this error:
Assets/Scripts/SettingsManager.cs(48,39): error CS0117: 'JsonUtility' does not contain a definition for 'toJson'
Here is the C# file:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
public class SettingsManager : MonoBehaviour
{
public Toggle fullscreenToggle;
public Dropdown resolutionDropdown;
public Resolution[] resolutions;
public GameSettings gameSettings;
public Button applyButton;
void OnEnable()
{
gameSettings = new GameSettings();
fullscreenToggle.onValueChanged.AddListener(delegate { OnFullscreenToggle(); });
resolutionDropdown.onValueChanged.AddListener(delegate { OnResolutionChange(); });
applyButton.onClick.AddListener(delegate { OnApplyButtonClick(); });
resolutions = Screen.resolutions;
foreach (Resolution resolution in resolutions)
{
resolutionDropdown.options.Add(new Dropdown.OptionData(resolution.ToString()));
}
}
public void OnFullscreenToggle()
{
gameSettings.fullScreen = Screen.fullScreen = fullscreenToggle.isOn;
}
public void OnResolutionChange()
{
Screen.SetResolution(resolutions[resolutionDropdown.value].width, resolutions[resolutionDropdown.value].height, Screen.fullScreen);
}
public void OnApplyButtonClick()
{
SaveSettings();
}
public void SaveSettings()
{
string jsonData = JsonUtility.toJson(gameSettings, true);
File.WriteAllText(Application.persistentDataPath + "/gamesettings.json",jsonData);
}
public void LoadSettings()
{
}
}

You have a typo. Write ToJson with uppercase T
JsonUtility.ToJson

Related

Having trouble with an Error CS1031: Type expected [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Having trouble. Can't find where I messed up. If anyone can spot the issue I'd appreciate it greatly. The error is: Error CS1031: Type expected
I've tried running the Unity Debug thing on the script with no luck. I just genuinely don't see an issue.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class AiStateMachine :
{
public AiState[] states;
public AiAgent agent;
public AiStateId currentState;
public AiStateMachine(AiAgent agent)
{
this.agent = agent;
int numStates = System.Enum.Getnames(typeof(AiStateId)).Length;
states = new AiState[numStates];
}
public void RegisterState(AiState state)
{
int index = (int)state.GetId();
states[index] = state;
}
public AIStateMachine GetState(AiStateId stateId)
{
int index = (int)stateId;
return states[index];
}
public void Update()
{
GetState(currentState)?.Update(agent);
}
public void ChangeState(AiStateId newState)
{
GetState(currentState)?.Exit(agent);
currentState = newState;
GetState(currentState)?.Enter(agent);
}
}
You have a dangling colon (:) after the class name. Since you aren't inheriting any class, you shouldn't have the colon there:
public class AiStateMachine
{
// ":" removed here ----^
While it isn't necessarily helpful in this particular case I want to mention that if you google the error code there is usually some info from Microsoft to help some. Here's the one for this error:
https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs1031
In this case though it looks like you have a class that is expecting to derive from something by having a colon at the end of the class declaration without any info after it:
public class AiStateMachine :
Try removing the colon or adding whatever the class should inherit from. This page also has some great info to brush up on inheritance if needed:
https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/tutorials/inheritance

error CS1061 in Unity (C#) ; I want to change sprite in script [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm going to make a system that changes skin in the game.
Whenever I press the button, I try to change the skin sample image to the specified image (on the inspector), but there is an error.
"error CS1061: 'Sprite' does not contain a definition for 'sample' and no accessible extension method 'sample' accepting a first argument of type 'Sprite' could be found (are you missing a using directive or an assembly reference?)"
I don't know why I get this error even though I first designated sample as Sprite.
Help me please.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.Runtime.Versioning;
using UnityEngine;
using UnityEngine.UI;
[System.Serializable]
public class Samples
{
public Sprite sample;
}
[System.Serializable]
public class Changes
{
[TextArea]
public string changes;
}
[System.Serializable]
public class Skin
{
[TextArea]
public string name;
public string explain;
}
public class change_skin : MonoBehaviour
{
[SerializeField] private SpriteRenderer sprite_sample;
[SerializeField] private Text txt_changes;
[SerializeField] private Text txt_skin_name;
[SerializeField] private Text txt_skin_explain;
private int count = 0;
private int num = 0;
[SerializeField] private Changes[] change_thing;
[SerializeField] private Skin[] skins;
[SerializeField]
public Samples[] sample_image;
[Serializable]
public struct Samples
{
[SerializeField]
public Sprite[] sample_thing;
}
void Start()
{
sprite_sample.sprite = sample_image[count].sample_thing[num].**sample**;
txt_changes.text = change_thing[num].changes;
txt_skin_name.text = skins[count].name;
txt_skin_explain.text = skins[count].explain;
}
public void bt_changes_right()
{
if (num >= change_thing.Length)
{
num = 0;
}
num++;
txt_changes.text = change_thing[num].changes;
sprite_sample.sprite = sample_image[count].sample_thing[num].**sample**;
}
sample_thing is a Sprite, and I think you are trying to acess sample inside the Sprite object with this:
sprite_sample.sprite = sample_image[count].sample_thing[num].**sample**;
Try stop on sample_thing. like that:
sprite_sample.sprite = sample_image[count].sample_thing[num];
See whats happens if you change it from Start() and from bt_changes_right()

Two methods, do the same thing, in different ways [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Hi I'm new to programming and I got this code:
public void Print(out string dataToPrint)
{
//code....
dataToPrint = "some text here"
}
And:
public string dataToPrint()
{
//code
return "some text here"
}
Which one will be used today, and which example will a professional programmer will use and what is the fastest in terms of performance?
In terms of performance there is nearly no difference, you can try it by running the each method inside a loop and test the time each can take, or use benchmark.net .
I tried it with Benchmark.net using following code:
public class Class1
{
public void Print(out string dataToPrint)
{
dataToPrint = "some text here";
}
public string Print()
{
return "some text here";
}
[Benchmark]
public void one()
{
string data;
Print(out data);
}
[Benchmark]
public void two()
{
Print();
}
}
class Program
{
static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<Class1>();
}
}
And the result was :
As you can see the difference is too small, so you shouldn't consider it in your case, but I prefer the second form for readability, however, for other cases try using same procedure and find out.

Class with same code and send data [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have this C# class to send the data from DriveInfo Class to my window form:
using System;
public class FileSystemInfo
{
public string CheckTotalFreeSpace()
{
System.IO.DriveInfo DInfo = new System.IO.DriveInfo(#"C:\");
return DInfo.TotalFreeSpace.ToString();
}
public string CheckVolumeLabel()
{
System.IO.DriveInfo DInfo = new System.IO.DriveInfo(#"C:\");
return DInfo.VolumeLabel.ToString();
}
}
I want to send huge data from one class (see my example) into my form class (maybe labels or ListBox Control), by using a a good way to solve this issue. Also I don't want to put this line of code into separate c# class method:
System.IO.DriveInfo DInfo
The basic issue for me is: deal and show many info about my computer so I need to put all this info into one structure or something else.
You may use Lazy class:
using System.IO;
public class FileSystemInfo
{
private readonly Lazy<DriveInfo> dInfo =
new Lazy<DriveInfo>(() => new DriveInfo(#"C:\"));
public string CheckTotalFreeSpace()
{
return dInfo.Value.TotalFreeSpace.ToString();
}
public string CheckVolumeLabel()
{
return dInfo.Value.VolumeLabel.ToString();
}
}

Object reference cannot fix [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Error CS0120 An object reference is required for the non-static field,
method, or property 'Blip.Sprite' HeliBoatMissions
namespace heliBoatMissions
{ }
public class Radar
{
void Marker()
{
new Blip(new Vector3(-699.4645f, -1448.289f, 5.000523f),
Blip.Sprite = BlipSprite.Helicopter);
}
}
I Think this is your issue
namespace heliBoatMissions
{ }
Change it to this
namespace heliBoatMissions
{
UPDATE
Also I noticed that you do not assign this
new Blip(new Vector3(-699.4645f, -1448.289f, 5.000523f),
to a variable. Nor do you end it with a semicolon.
I think something like this might be what you're after?
namespace heliBoatMissions
{
public class Radar
{
public object BlipSprite { get; private set; }
void Marker()
{
var v = new Blip(new Vector3(-699.4645f, -1448.289f, 5.000523f));
v.Sprite = BlipSprite.Helicopter;
}
}
}

Categories

Resources