How do I modify variables from a different class that belongs to the same C# file?
I'm making a small database that's used to save Dropdown values (integers) in a XML database.. currently it's set up like this:
public class XMLManager : MonoBehaviour {
//other code before the variable below public VideoSettings vSettingsDB;
//save function
public void SaveItems()
{
//Configure save
itemEntry.configSettings(); //This is what I basically am trying to aim for, but with no luck
//open a new xml file
XmlSerializer serializer = new XmlSerializer(typeof(VideoSettings));
FileStream stream = new FileStream(Application.persistentDataPath, FileMode.Create);
serializer.Serialize(stream, vSettingsDB);
stream.Close();
}
//load function (not done yet)
}
[System.Serializable]
public class itemEntry
{
public int resolutionValue, shadowValue, lightValue, textureValue;
public bool vSyncValue;
UserInterfaceScript UI;
public void configSettings()
{
UI = GameObject.Find("GUI").GetComponent<UserInterfaceScript>();
resolutionValue = UI.resolutionSetting.value;
shadowValue = UI.shadowQuality.value;
lightValue = UI.lightMode.value;
textureValue = UI.textureQuality.value;
vSyncValue = UI.VSync.isOn;
}
}
[System.Serializable]
public class VideoSettings
{
public List<itemEntry> list = new List<itemEntry>();
}
I need some help as I can't find anything related to modifying variables of another class in the same file :/
Or maybe to modify directly the items in the list.. I'm just really confused
Don't get why do you need to save a VideoSettings instance with a list of itemEntries ins$$anonymous$$d of a single itemEntry, what s the point on all this. Anyway
public void SaveItems()
{
//Configure save
vSettingsDB.list[someIndex].configSettings(); //of course that requires that the vSettingsDB has been initialized allready and contains at least one itemEntry in the list
//else if you want to populate the list in here:
vSettingDB.list.Add(new itemEntry());
vSettingDB.list[vSettingDB.list.Count - 1].configSettings(); //that requires that the vSettingsDB has been initialized allready, then adds a new itemEntry and configures it
//itemEntry.configSettings(); //This is what I basically am trying to aim for, but with no luck
//open a new xml file
XmlSerializer serializer = new XmlSerializer(typeof(VideoSettings));
FileStream stream = new FileStream(Application.persistentDataPath, File$$anonymous$$ode.Create);
serializer.Serialize(stream, vSettingsDB);
stream.Close();
}
If you add itemEntries on the same frame (same time) all would be identical so there is no point on it.
Note1: classes located on the same file does NOT share anything just the place they are written other than that it is same thing as accessing any class on any file (unless they be inside different namespaces,then you need to define it).
Note2: to access a class you need a reference to the insance you are trying to access as long it is not static. If no instance exist then you need to create one with the "new" keyword.
@PizzaPie I found a solution using a little plugin I found in the asset store, so none of the code above is used anymore. Thanks anyways, and good luck on your projects :)
Answer by Andre_51X · May 01, 2017 at 12:56 PM
I found a solution to my issue using this plugin: http://u3d.as/5tv It's compatible with Unity 5.6 and it's extremely easy to use
Your answer
Follow this Question
Related Questions
Can Static classes have static lists? 1 Answer
How to reference a class in an enumerator 1 Answer
Problem with Lists and Remove 0 Answers
Collection was modified; enumeration operation may not compute 0 Answers
How get a count of Lists assigned 0 Answers