Ok I am writing a save game function and I am using this example as my baseline : http://wiki.unity3d.com/index.php?title=Save_and_Load_from_XML
problem is when I try to use it in my script the compiler does not recognize SerializeObject. It compiles the example just fine so I know I am screwing up somewhere. I would very much appreciate if someone can point out my mistake : P
I’ll include the whole script since I probably forgot to import something or get something set up properly. But the function in particular that is not working is the Save() function.
My Code:
#pragma strict
import System.Collections.Generic;
import System.Xml;
import System.Xml.Serialization;
import System.IO;
import System.Text;
class Shipwrite extends MonoBehaviour{//Begin Class
/*
The players comander class
respomsible for interpreting player Input
and converting it into commands for Cores
*/
var PanSpeed : float = 0.01;
var FreeCamera = true;
var SelectedCore : Core;
var ShipType : ShipClass;
var CurrentToolSet : String = "Hulls";
var CurretnTool : Transform;
var MouseInMenu = false;
var _FileLocation : String = Application.dataPath;
var ShipName : String = "Ship Name";
private var ToolList : List.<Transform> = new List.<Transform>();
private var Ghost : Transform;
class ShipSave
{
var Data : List.<PartData>;
}
class PartData
{
var PartName : String;
var PartPos : Vector3;
var PartRot : Quaternion;
}
class WeaponData extends PartData
{
var Group : WeaponGroup;
}
function Start ()
{
ShipName = "Ship Name";
}
function Update ()
{
if(!MouseInMenu)
{
FindGrid();
CheckKeys();
}
}
function CheckKeys()
{
//Zoom
if(Input.GetAxis("Zoom") > 0)
{
Camera.main.orthographicSize -= 5;
if(Camera.main.orthographicSize < 5)
{
Camera.main.orthographicSize = 5;
}
}
if(Input.GetAxis("Zoom") < 0)
{
Camera.main.orthographicSize += 5;
if(Camera.main.orthographicSize > 50)
{
Camera.main.orthographicSize = 50;
}
}
if(Input.GetAxis("DragCamera") > 0)
{
Camera.main.transform.position += Vector3(-Input.GetAxis("MouseX") * PanSpeed *Camera.main.orthographicSize, -Input.GetAxis("MouseY") * PanSpeed * Camera.main.orthographicSize, 0);
}
//Vars for placing and removing blocks
var MouseHit = GetMouseHit();
var GridLoc : Vector3 = GetMouseHit().point;
//Remove Blocks
if(Input.GetAxis("Missiles") > 0)
{
if(MouseHit.collider.tag != "Grid")
{
Destroy(MouseHit.collider.gameObject);
}
}
//Place Blocks
if(Input.GetAxis("Turret") > 0)
{
if(CurretnTool != null)
{
var NewPart : Transform;
if(Ghost.GetComponent(Hull).Module != ModuleType._NeedFloor)
{
if(MouseHit.collider.tag == "Grid")
{
GridLoc.x = Mathf.Round(GridLoc.x);
GridLoc.y = Mathf.Round(GridLoc.y);
GridLoc.z = 2;
NewPart = Instantiate(CurretnTool, GridLoc, Quaternion.identity);
NewPart.transform.parent = SelectedCore.transform;
}
}
else
{
if(MouseHit.collider.tag != "Grid")
{
if(MouseHit.collider.GetComponent(Hull).Module == ModuleType._Floor)
{
GridLoc.x = Mathf.Round(GridLoc.x);
GridLoc.y = Mathf.Round(GridLoc.y);
GridLoc.z = 2;
NewPart = Instantiate(CurretnTool, GridLoc, Quaternion.identity);
NewPart.transform.parent = SelectedCore.transform;
}
}
}
}
}
}
function FindGrid()
{
if(Ghost != null)
Destroy(Ghost.gameObject);
if(CurretnTool != null)
{
var GridLoc : Vector3 = GetMouseHit().point;
GridLoc.x = Mathf.Round(GridLoc.x);
GridLoc.y = Mathf.Round(GridLoc.y);
GridLoc.z = 2;
Ghost = Instantiate(CurretnTool, GridLoc, Quaternion.identity);
Ghost.collider2D.enabled = false;
Debug.Log(CurretnTool.name);
}
}
//Check what is under mouse
function GetMouseHit() : RaycastHit2D
{
// get mouse location 2D
var mousePos = Input.mousePosition;
//create ray from mouse and... yeah actualy I dont know how this works.
var ray : Ray = camera.main.ScreenPointToRay (mousePos);
//Used to store what we hit
var hitInfo : RaycastHit2D;
hitInfo = Physics2D.GetRayIntersection(ray);
return hitInfo;
}
function SaveShip()
{
if(ShipName == "")
{
ShipName = "Ship Name";
}
var SaveData : ShipSave;
var _data : String;
// Time to creat our XML!
_data = SerializeObject(SaveData);
// This is the final resulting XML from the serialization process
var writer : StreamWriter;
var t : FileInfo = new FileInfo(_FileLocation+"/"+ ShipName);
if(!t.Exists)
{
writer = t.CreateText();
}
else
{
t.Delete();
writer = t.CreateText();
}
writer.Write(_data);
writer.Close();
Debug.Log("File written.");
//TODO SAVE THE SHIP!
}
function OnGUI()
{
var BoxHight = Screen.height * 0.2;
var BoxWidth = Screen.width;
GUI.color.a = 200;
GUI.backgroundColor = Color.green;
GUI.backgroundColor.a = 200;
var ToolBox : Rect = Rect(0, Screen.height - BoxHight, BoxWidth, BoxHight);
GUI.Box(ToolBox, "");
GUI.Box(Rect(0,0,BoxHight,BoxHight), "");
if(Rect(0,0,BoxWidth,BoxHight).Contains(Input.mousePosition) || Rect(0,Screen.height - BoxHight,BoxHight,BoxHight).Contains(Input.mousePosition))
{
MouseInMenu = true;
}
else
{
MouseInMenu = false;
}
GUILayout.BeginArea(Rect(0,0,BoxHight,BoxHight));
GUILayout.BeginVertical();
ShipName = GUILayout.TextField(ShipName,20);
GUILayout.FlexibleSpace();
if(GUILayout.Button("Save"))
{
SaveShip();
}
GUILayout.FlexibleSpace();
if(GUILayout.Button("Save and Test"))
{
SaveShip();
}
GUILayout.FlexibleSpace();
if(GUILayout.Button("Menu"))
{
}
GUILayout.EndHorizontal();
GUILayout.EndArea();
var SelectionBox : Rect = Rect(0, Screen.height - BoxHight, BoxHight, BoxHight);
GUILayout.BeginArea(SelectionBox);
GUILayout.BeginVertical();
if(GUILayout.Button("Hulls"))
{
CurrentToolSet = "Hulls";
}
GUILayout.FlexibleSpace();
if(GUILayout.Button("Modules"))
{
CurrentToolSet = "Modules";
}
GUILayout.FlexibleSpace();
if(GUILayout.Button("Weapons"))
{
CurrentToolSet = "Weapons";
}
GUILayout.EndVertical();
GUILayout.EndArea();
var MainBox : Rect = Rect(BoxHight, Screen.height - BoxHight, BoxWidth - BoxHight, BoxHight);
GUILayout.BeginArea(MainBox);
GUILayout.BeginHorizontal();
var i = 0;
if(CurrentToolSet == "Hulls")
{
for(i = 0; i < ShipType.Hulls.Length;)
{
if(GUILayout.Button(Resources.Load("Materials/" + ShipType.Hulls*.name, typeof(Texture2D)), GUILayout.Width(BoxHight), GUILayout.Height(BoxHight)))*
-
{*
_ CurretnTool = ShipType.Hulls*;_
_ }_
_ i++;_
_ }_
_ }*_
* if(CurrentToolSet == “Modules”)*
* {*
* for(i = 0; i < ShipType.Modules.Length;)*
* {*
_ if(GUILayout.Button(Resources.Load(“Materials/” + ShipType.Modules*.name, typeof(Texture2D)), GUILayout.Width(BoxHight), GUILayout.Height(BoxHight)))
{
CurretnTool = ShipType.Modules;
}
i++;
}
}*_
* if(CurrentToolSet == “Weapons”)*
* {*
* for(i = 0; i < ShipType.Weapons.Length;)*
* {*
_ if(GUILayout.Button(Resources.Load(“Materials/” + ShipType.Weapons*.name, typeof(Texture2D)), GUILayout.Width(BoxHight), GUILayout.Height(BoxHight)))
{
CurretnTool = ShipType.Weapons;
}
i++;
}
}*_
* GUILayout.EndHorizontal();*
* GUILayout.EndArea();*
}
}//End Class
Thanks in advance : D