- Home /
How can I access this class from another script?
Hi!
I am trying to work on a save game system for Unity and the community. I have, however, run into a slight problem. I am trying to access a serializable class from another script, but it is saying "The name "SavingSystemComponent.VectorsComponent" does not denote a valid type ("not found")." This script is going to be working several times in a 'for' loop, so the data will be overwritten. I have a SaveingSystemComponent that can use the class for reference, but I have no idea how to get it to correctly use it. I have made the class public and have tried accessing it with a cached component, but nothing is working. Hopefully, as the Unity community, you will be able to set me right!
Here is the code:
SavingSystemManager:
#pragma strict
var SavingSystemComponents : SavingSystemComponent[];
var path : String;
function Start () {
path = PlayerPrefs.GetString("LoadFilePath","");
Save(path);
yield WaitForSeconds(5);
Load(path);
}
public function Save (path : String) {
SavingSystemComponents = FindObjectsOfType(SavingSystemComponent) as SavingSystemComponent[];
for (var SSC : SavingSystemComponent in SavingSystemComponents)
{
SSC.SavePosition(path);
}
}
public function Load(path : String)
{
var a : int = 0;
var loadfs : FileStream;
var loadformatter : BinaryFormatter = new BinaryFormatter();
//Buildings
var savedatafilesBuildings : String[] = Directory.GetFiles(path + "/Buildings/", "*.savedata");
for (a = 0; a <savedatafilesBuildings.Length;a++)
{
loadfs = new FileStream(savedatafilesBuildings[a], FileMode.Open);
var loaddata : SavingSystemComponent.VectorsComponent = loadformatter.Deserialize(loadfs);
print(loaddata.posX.ToString());
loadfs.Close();
}
/*//Entities
var savedatafilesEntities : String[] = Directory.GetFiles(path + "/Entities/", "*.savedata");
for (a = 0; a <savedatafilesEntities.Length;a++)
{
print(savedatafilesEntities[a]);
}
//Players
var savedatafilesPlayers : String[] = Directory.GetFiles(path + "/Players/", "*.savedata");
for (a = 0; a <savedatafilesPlayers.Length;a++)
{
print(savedatafilesPlayers[a]);
}*/
}
/*public function LoadPosition(savePath : String) {
var loadfs : FileStream = new FileStream(savePath + "/Buildings/Buildings.savedata", FileMode.Open);
var loadformatter : BinaryFormatter = new BinaryFormatter();
var loaddata : VectorsManager = loadformatter.Deserialize(loadfs);
print(loaddata.posX.ToString());
loadfs.Close();
}*/
SavingSystemComponent:
#pragma strict
import System.IO;
import System.Runtime.Serialization.Formatters.Binary;
public enum SaveType {Null=0, Building=1, Player=2,Entity=3}
public var saveType : SaveType = SaveType.Null;
public enum BuildingType {Null=0,Wall=1, Door=2,Window=3, Floor=4, Stairs=5, WinBarricade=6, Campfire=7}
public var buildingType : BuildingType = BuildingType.Null;
public enum BuildingMat {Null=0,Wood=1, ReinforcedWood=2,Steel=3}
public var buildingMat : BuildingMat = BuildingMat.Null;
public enum CampfireType {Null=0,Simple=1, Complex=2}
public var campfireType : CampfireType = CampfireType.Null;
var referenceClass : boolean = false;
private var savePath : String = "";
function Start () {
/*if (saveType == SaveType.Null)
{
print("This Save Component SaveType is null! Please assign a SaveType!");
enabled = false;
}
if (saveType == SaveType.Building && buildingType == BuildingType.Wall && buildingMat == BuildingMat.Wood)
{
print("Building Type:" + buildingMat + " " + buildingType);
}*/
}
public function SavePosition (path : String) {
if (referenceClass)
{
return;
}
var savefs : FileStream;
if (File.Exists(path + "/Buildings/Buildings_"+transform.position.x+"."+transform.position.y+"."+transform.position.z+".savedata"))
{
savefs = new FileStream(path + "/Buildings/Buildings_"+transform.position.x+"."+transform.position.y+"."+transform.position.z+".savedata", FileMode.Open);
}
else if (File.Exists(path + "/Buildings/Buildings_"+transform.position.x+"."+transform.position.y+"."+transform.position.z+".savedata") == false)
{
savefs = new FileStream(path + "/Buildings/Buildings_"+transform.position.x+"."+transform.position.y+"."+transform.position.z+".savedata", FileMode.Create);
}
var saveformatter : BinaryFormatter = new BinaryFormatter();
var savedata : VectorsComponent = new VectorsComponent();
savedata.posX = transform.position.x;
savedata.posY = transform.position.y;
savedata.posZ = transform.position.z;
savedata.rotW = transform.rotation.w;
savedata.rotX = transform.rotation.x;
savedata.rotY = transform.rotation.y;
savedata.rotZ = transform.rotation.z;
saveformatter.Serialize(savefs, savedata);
savefs.Close();
}
@System.Serializable
public class VectorsComponent
{
public var posX : float;
public var posY : float;
public var posZ : float;
public var rotW : float;
public var rotX : float;
public var rotY : float;
public var rotZ : float;
}
Cheers, Stormy102
This is one of the most common questions asked here, there are nearly endless answers that exist already that could probably help you.
You've posted more than 100 lines of code, but you didn't give a line number for the error you're seeing. While I'm happy to see code and an error message posted, it is unlikely that someone will try to parse through all of that code for the one line that is causing the issue.
Additionaly, please do not 'bump' your questions. This is not a forum, and it would still be frowned upon if it was. You waited approximately 40 $$anonymous$$utes to bump the question, which by most standards is not very long at all. Please show patience in the future.
That being said, you've defined VectorsComponent as a class inside of the SavingSystemComponent class, but I didn't see any place where you declared a variable of that type within SavingSystemComponent, so that's a good place to start.
$$anonymous$$aybe, problem in line:
var SavingSystemComponents : SavingSystemComponent[];
Your variable name and class name is match. It's not correct. Change name, for example:
var mySavingSystemComponents : SavingSystemComponent[];
Answer by Kiwasi · Oct 27, 2014 at 07:10 PM
Step 1: Change line 32 to
var loaddata : VectorsComponent = loadformatter.Deserialize(loadfs);
Step 2: Check out this link
Your answer
Follow this Question
Related Questions
How to Typecast JS Variables as C# Classes? 0 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Should I manually delete my custom class instances? 1 Answer
javascript singleton 1 Answer
making my classes usable? 1 Answer