Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by Stormy102 · Oct 27, 2014 at 05:36 PM · javascriptclassserialize

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

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Stormy102 · Oct 27, 2014 at 05:53 PM 0
Share

bumpity bump bump!

avatar image Habitablaba · Oct 27, 2014 at 06:16 PM 1
Share

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.

avatar image Stormy102 · Oct 27, 2014 at 06:30 PM 0
Share

anyone got any ideas?

avatar image zharik86 · Oct 27, 2014 at 06:53 PM 0
Share

$$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[];

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges