- Home /
Question by
Wolftic · Feb 16, 2014 at 04:12 PM ·
nullreferenceexceptionsaveloadsystemnullreference
Getting error on instatiating.
I get a nullreferenceexception error when I try to load my world from a .txt file. I'm using the script from erdroy with some changes: using UnityEngine; using System.Collections; using System; using System.Collections.Generic; using System.Text.RegularExpressions; using System.IO; using System.Text;
public class Save : MonoBehaviour {
/// <summary>
/// The objects.
/// </summary>
public GameObject[] Objects;
public string SaveString;
/// <summary>
/// The objects loaded.
/// </summary>
public string LoadString;
/// <summary>
/// GameObjects
/// </summary>
public GameObject[] Objects_;
public string savename = "TEST";
void Start(){
DontDestroyOnLoad (transform.gameObject);
}
void Update () {
if(Input.GetKeyDown(KeyCode.F5))
{
SaveGame(savename);
}
}
public void SaveGame(string savename){
Objects = GameObject.FindGameObjectsWithTag("Savable");
SaveString = "";
for(int i = 0; i < Objects.Length; i++)
{
SaveString +=
Objects[i].name
+ ","+
Objects[i].transform.position.x + "|" + Objects[i].transform.position.y + "|" +Objects[i].transform.position.z + "|"
+ ","+
Objects[i].transform.rotation.x + "|" + Objects[i].transform.rotation.y + "|" +Objects[i].transform.rotation.z + "|"
+ ","+
Objects[i].transform.localScale.x + "|" + Objects[i].transform.localScale.y + "|" +Objects[i].transform.localScale.z + "|"
+ ";";
}
FileOperations.WriteToFile(savename+".txt", SaveString);
Debug.Log ("SAVED");
}
public void LoadGame(string savename){
LoadString = FileOperations.ReadFile(savename+".txt");
string[] ObjectsLoaded = LoadString.Split(';');
foreach(string record in ObjectsLoaded)
{
string[] recordSelected = record.Split(',');
string naz, poz1, rot1, scl1;
string[] poz, rot, scl;
Vector3 position;
Vector3 scale;
Quaternion rotation;
naz = recordSelected[0].ToString();
Debug.Log("Loaded: "+naz);
poz1 = recordSelected[1].ToString();
Debug.Log("Loaded: "+poz1);
rot1 = recordSelected[2].ToString();
Debug.Log("Loaded: "+rot1);
scl1 = recordSelected[3].ToString();
Debug.Log("Loaded: "+scl1);
poz = poz1.Split('|');
rot = rot1.Split('|');
scl = scl1.Split('|');
position.x = Convert.ToSingle(poz[0]);
position.y = Convert.ToSingle(poz[1]);
position.z = Convert.ToSingle(poz[2]);
rotation.x = Convert.ToSingle(rot[0]);
rotation.y = Convert.ToSingle(rot[1]);
rotation.z = Convert.ToSingle(rot[2]);
rotation.w = 1;
scale.x = Convert.ToSingle(scl[0]);
scale.y = Convert.ToSingle(scl[1]);
scale.z = Convert.ToSingle(scl[2]);
if(naz == Objects_[0].name || naz == Objects_[0].name + "(Clone)")
{
Transform go = Instantiate(Objects_[0], position, rotation) as Transform;
go.localScale = scale;
}
if(naz == Objects_[1].name || naz == Objects_[1].name + "(Clone)")
{
Transform go = Instantiate(Objects_[1], position, rotation) as Transform;
go.localScale = scale;
}
if(naz == Objects_[2].name || naz == Objects_[2].name + "(Clone)")
{
Transform go = Instantiate(Objects_[2], position, rotation) as Transform;
go.localScale = scale;
}
if(naz == Objects_[3].name || naz == Objects_[3].name + "(Clone)")
{
Transform go = Instantiate(Objects_[3], position, rotation) as Transform;
go.localScale = scale;
}
if(naz == Objects_[4].name || naz == Objects_[4].name + "(Clone)")
{
Transform go = Instantiate(Objects_[4], position, rotation) as Transform;
go.localScale = scale;
}
if(naz == Objects_[5].name || naz == Objects_[5].name + "(Clone)")
{
Transform go = Instantiate(Objects_[5], position, rotation) as Transform;
go.localScale = scale;
}
/*if(naz == Objects_[6].name || naz == Objects_[6].name + "(Clone)")
{
Transform go = Instantiate(Objects_[6], position, rotation) as Transform;
go.localScale = scale;
}*/
}
}
}
//By Erdroy (c) HLTC
//
I'm getting an error at "go.localScale = scale;", saying:
Save.LoadGame (System.String savename) (at Assets/Scripts/Save.cs:114) PlayerController.Start () (at Assets/Scripts/PlayerController.cs:50) When I remove all of the scale thingy's I get this error:NullReferenceException: Object reference not set to an instance of an object
Save.LoadGame (System.String savename) (at Assets/Scripts/Save.cs:77) PlayerController.Start () (at Assets/Scripts/PlayerController.cs:50) line 77: "poz1 = recordSelected[1].ToString();" Please help me, I need this for my game.IndexOutOfRangeException: Array index is out of range.
Comment
Your answer
Follow this Question
Related Questions
Scene Saving 1 Answer
simple checkpoint/save/load system in javascript 4 Answers
Problem with Loading States 0 Answers
half life save/load system ? 1 Answer