- Home /
GameObject from foreach loop dosn't have transform?,
I'm trying to make the following code work:
public void saveData()
{
if (mainHand)
{
GameObject[] saveObjects = GameObject.FindGameObjectsWithTag("deletableobject");
SandboxObject sandboxObject = new SandboxObject();
foreach (GameObject obj in saveObjects)
{
Transform objTransform = obj.transform;
deleteOnHit objData = obj.GetComponent<deleteOnHit>();
sandboxObject.pos.Add(objTransform.position);
sandboxObject.rot.Add(objTransform.rotation.eulerAngles);
sandboxObject.scale.Add(objTransform.localScale);
sandboxObject.matNum.Add(objData.matNum);
sandboxObject.matFileNum.Add(objData.matFileNum);
sandboxObject.meshNum.Add(objData.meshNum);
}
string saveJson = JsonUtility.ToJson(sandboxObject);
File.WriteAllText(Application.dataPath + "saveFile", saveJson);
Debug.Log("saved as: " + saveJson);
}
}
This code is supposed to save all object with tag of "deletableobject" into a single json file, but on line 11, it throws me a NullReferenceExeption.
I have 3 object with tag"deletableobject" on scene, and they all have script deleteOnHit.
What's causing this error? Please let me know if you do.
Thank you!
UPDATE: I realized that this problem happens with a for loop as well:
public void saveData()
{
if (mainHand)
{
GameObject[] saveObjects = GameObject.FindGameObjectsWithTag("deletableobject");
SandboxObject sandboxObject = new SandboxObject();
for (int i = 0; i < saveObjects.Length; i++)
{
Transform objTransform = saveObjects[i].transform;
deleteOnHit objData = saveObjects[i].GetComponent<deleteOnHit>();
sandboxObject.pos.Add(objTransform.position);
sandboxObject.rot.Add(objTransform.rotation.eulerAngles);
sandboxObject.scale.Add(objTransform.localScale);
sandboxObject.matNum.Add(objData.matNum);
sandboxObject.matFileNum.Add(objData.matFileNum);
sandboxObject.meshNum.Add(objData.meshNum);
}
string saveJson = JsonUtility.ToJson(sandboxObject);
File.WriteAllText(Application.dataPath + "saveFile", saveJson);
Debug.Log("saved as: " + saveJson);
}
}
Answer by noyco · Apr 16, 2020 at 11:55 AM
Nvm I just found out that it was the problem in the "SandboxObject" class.
Used to be:
private class SandboxObject
{
public List<Vector3> pos;
public List<Vector3> rot;
public List<Vector3> scale;
public List<int> matNum;
public List<int> matFileNum;
public List<int> meshNum;
}
Fixed:
private class SandboxObject
{
public List<Vector3> pos = new List<Vector3>();
public List<Vector3> rot = new List<Vector3>();
public List<Vector3> scale = new List<Vector3>();
public List<int> matNum = new List<int>();
public List<int> matFileNum = new List<int>();
public List<int> meshNum = new List<int>();
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612231502im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
NULL REFERENCE EXCEPTION,NULL REFERENCE EXCEPTION while accessing child objects unity2D 0 Answers
Why does this Instantiate give a Null Reference Exception? 1 Answer
Trying to call a function from a dynamically created object 1 Answer
How to move a Game Object from a script not attached to it. 1 Answer