So, i am making a save load scene system that loads data into a .txt file which holds each object. I have a few objects that can be interacted with and they have a script which contains a “ConnectedObjects” List. but it keeps coming up with the error in the title. this is only when loading the scene. here is a part of the code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class LoadLevelSystem : MonoBehaviour
{
string[] objectsToLoad;
string path = Application.dataPath + "/Scene.txt";
int propertyID;
Vector3 objectPos;
Quaternion objectRot;
int objectPrefabID;
int indevObjectID;
public List<int> connectedObjects = new List<int>();
public GameObject levelGO;
public GameObject[] objectsToPlace;
public void ReadScene() {
objectsToLoad = File.ReadAllLines(path);
foreach(string line in objectsToLoad) {
string[] properties = line.Split("|");
foreach (string property in properties) {
propertyID += 1;
Debug.Log(property);
if (propertyID == 1) {
string properti = property.Replace("(", "");
properti = properti.Replace(")", "");
string [] indevidualNumberStrings = properti.Split(", ");
objectPos.x = float.Parse(indevidualNumberStrings[0]);
objectPos.y = float.Parse(indevidualNumberStrings[1]);
objectPos.z = float.Parse(indevidualNumberStrings[2]);
} else if (propertyID == 2) {
string properti = property.Replace("(", "");
properti = properti.Replace(")", "");
string [] indevidualNumberStrings = properti.Split(", ");
objectRot = Quaternion.Euler(
float.Parse(indevidualNumberStrings[0]),
float.Parse(indevidualNumberStrings[1]),
float.Parse(indevidualNumberStrings[2])
);
} else if (propertyID == 3) {
objectPrefabID = int.Parse(property);
} else if (propertyID == 4) {
indevObjectID = int.Parse(property);
} else if (propertyID == 5) {
string properti = property.Replace("(", "");
properti = properti.Replace(")", "");
string [] indevidualNumberStrings = properti.Split(", ");
foreach (string numberString in indevidualNumberStrings) {
connectedObjects.Add(int.Parse(numberString));
}
}
}
propertyID = 0;
GameObject obj = Instantiate(objectsToPlace[objectPrefabID], objectPos, objectRot);
obj.GetComponent<identifier>().individualObjectID = indevObjectID;
obj.transform.parent = levelGO.transform;
foreach (int connectedObjectIndevID in connectedObjects) {
foreach(Transform childObj in levelGO.transform) {
if (childObj.gameObject.GetComponent<identifier>().individualObjectID == connectedObjectIndevID) {
if (obj.GetComponent<identifier>().ID == 0){
childObj.gameObject.GetComponent<interactableScript>().connectedObjects.Add(childObj.gameObject);
}
if (obj.GetComponent<identifier>().ID == 2){
childObj.gameObject.GetComponent<buttonScript>().connectedObjects.Add(childObj.gameObject);
}
}
}
}
}
}
}
I am trying to get the connectedObjects List to add the object to the list but i cant getting working i have tried for like 2 hours. this is my last resort.
New contributor
RedSkull is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1