- Home /
Need Help - Last Problem Converting PC to Android - Using JS to read CS
I managed to fix all of my code that was conflicting with Android specific rules (dynamics vs static, etc). All but one that is...
I am not good with C# (..yet) so all my scripts are done in JS. All but one that is...
I have a C# script that uses the built in serialization to save/load my 'game saves'. I have a couple JS files that allow me to interact with the C# file. See the following link for full details:
http://forum.unity3d.com/threads/94997-Need-Help-Save-Load?p=619994&viewfull=1#post619994
Everything was working great in the PC/Mac version but I am having a problem with Android. If I could just get this one chunk working, I will understand how to fix the rest.
if(firstRun){
firstRun = false;
var script = GameObject.Find("Level").GetComponent("SaveAndLoad");
//Slot 1
script.SendMessage("Load","./saves/SaveData_1.agt");
saveInfo[0] = GameObject.Find("Level").GetComponent.<LoadData>().data;
//Slot 2
script.SendMessage("Load","./saves/SaveData_2.agt");
saveInfo[1] = GameObject.Find("Level").GetComponent.<LoadData>().data;
//Slot 3
script.SendMessage("Load","./saves/SaveData_3.agt");
saveInfo[2] = GameObject.Find("Level").GetComponent.<LoadData>().data;
//Slot 4
script.SendMessage("Load","./saves/SaveData_4.agt");
saveInfo[3] = GameObject.Find("Level").GetComponent.<LoadData>().data;
//Slot 5
script.SendMessage("Load","./saves/SaveData_5.agt");
saveInfo[4] = GameObject.Find("Level").GetComponent.<LoadData>().data;
//Slot 6
script.SendMessage("Load","./saves/SaveData_6.agt");
saveInfo[5] = GameObject.Find("Level").GetComponent.<LoadData>().data;
//Debug Info
for(var i=0; i<saveInfo.length; i++){
**Debug.Log("SlotSelection caching "+i+": "+saveInfo[i].levelReached);**
}
}
The error I am getting is:
Assets/Scripts/MainMenu/SlotSelection.js(47,71): BCE0019: 'levelReached' is not a member of 'Object'.
"levelReached" is a public variable defined in the SaveAndLoad.cs file. It is included with other variables and "packed up" into a single variable called "data". I'm not sure exactly how it works but the C# code looks like this:
**data = (SaveData)bformatter.Deserialize(stream);
GameObject.Find("Level").SendMessage("LoadData",data);**
If anyone can help me get the "levelReached" value out of "data", I can fix all the rest of my problems and finally test this thing....
Note:
This is at the top of the "SaveAndLoad.cs":
public class SaveData : ISerializable { // === Values ===
// Edit these during gameplay
public int month = 1;
public int day = 1;
public int year = 2011;
public string time = "12:00:00";
public int levelReached = 0;
public float completionPercent = 0.0f;
...
And yet I cannot use "SaveData" as a type such as:
var saveInfo = new SaveData[5];
$$anonymous$$aybe I am over complicating this??
I'm just trying to pass the "data" variable from the C# file to the JS file, then have a different JS file get one of the values out of the "data" variable from the first JS file. ie: data.levelReached, data.completionPercent, data.time, etc
When I try something like "print(data.levelReached)" I get an error about "levelReached" not being a member of "object". In the PC build, there was no error and the console/debug window said "1" letting me know I had reached level 1...
Answer by kingdutka · Sep 19, 2011 at 02:25 AM
The problem was partly answered by Warwick's post but one thing that I was also missing... The C# file was in the same folder with the JS files. I read that they cannot be loaded/executed at the same time and one needs to be in the "Standard Assets" or "Plugins" folder. I moved the C# script to the standard assets folder and I am now able to access "SaveAndLoad" and "SaveData" as a valid 'type' and as such, I can now do:
var saveInfo = new SaveData[5];
And voila, almost every single error I had just disappeared (now to replicate to the other files as needed and I'm done!)
Answer by Waz · Sep 18, 2011 at 11:28 PM
Your saveInfo variable is declared wrong. My guess is it is:
var saveInfo = new Array();
when it needs to be:
var saveInfo = new LoadData[5];
I tried that and it only changed my error from:
Assets/Scripts/$$anonymous$$ain$$anonymous$$enu/SlotSelection.js(47,71): BCE0019: 'levelReached' is not a member of 'Object'.
To:
Assets/Scripts/$$anonymous$$ain$$anonymous$$enu/SlotSelection.js(47,71): BCE0019: 'levelReached' is not a member of 'LoadData'.
Please note that it was working "as is" on PC/$$anonymous$$ac. It only stopped working when I switched to an Android environment...
Your answer
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
I need help with a script(brauche hilfe mit einen Script) 0 Answers
Objectives based on object appear. 2 Answers