- Home /
Access Violation (Read from location 00000000 caused an access violation)
I have a editor script I call FastSceneCreator which converts scenes into a prefab.
Here is the script:
public class FastSceneCreator : EditorWindow
{
[MenuItem("UNDERUNITY/Convert to Fast Scene %#.", false, 0)]
public static void Convert()
{
// EditorUtility.DisplayDialog("Convert to Fast Scene", "Converting...", "Okay");
CreateScenePrefab(new GameObject(SceneManager.GetActiveScene().name).transform);
}
public static void CreateScenePrefab(Transform p)
{
p.gameObject.AddComponent<FastScene>();
GameObject[] gameobjects = GameObject.FindObjectsOfType<GameObject>();
for (int i = 0; i < gameobjects.Length + 1; i++)
{
if (i < gameobjects.Length)
{
if (gameobjects[i].transform.parent == null)
{
gameobjects[i].transform.SetParent(p);
continue;
}
}
else
{
if (AssetDatabase.LoadAssetAtPath<GameObject>(SceneManager.GetActiveScene().path.Replace(".unity", "") + ".prefab") != true)
{
PrefabUtility.CreatePrefab(SceneManager.GetActiveScene().path.Replace(".unity", "") + ".prefab", p.gameObject);
}
else
{
p.GetComponent<FastScene>().Additive = AssetDatabase.LoadAssetAtPath<GameObject>(SceneManager.GetActiveScene().path.Replace(".unity", "") + ".prefab").GetComponent<FastScene>().Additive;
PrefabUtility.ReplacePrefab(p.gameObject, AssetDatabase.LoadAssetAtPath<GameObject>(SceneManager.GetActiveScene().path.Replace(".unity", "") + ".prefab"), ReplacePrefabOptions.ReplaceNameBased);
}
RemoveChildrenAndDelete(p);
break;
}
}
}
public static void RemoveChildrenAndDelete(Transform p)
{
EditorUtility.DisplayDialog("Convert to Fast Scene", "Finishing things up...", "...");
p.DetachChildren();
if (Resources.Load<FastSceneSettings>("fastsettings").NotifySceneConvert == true)
{
EditorUtility.DisplayDialog("Convert to Fast Scene", "Scene successfuly converted.", "Okay");
}
}
}
When I use the Convert to Fast Scene menu item, the editor hangs without going into "Not Responding", then it crashes.
If you need the error log, tell me!
You need to properly format your code, no one can decipher this bunch of trash in red text. Access violation means you're touching things you're not supposed to touch - you need to check your memory access.
I fixed it now. (The formatting of the code)
Answer by christoph_r · Aug 20, 2017 at 10:40 PM
I'd say for (int i = 0; i < gameobjects.Length + 1; i++)
in line 14 appears to be the issue.
Oh! Thanks! You made me realize that the Array "gameobjects" was filled by the children of the parent object, and when I set the parent of the child to another one, the gameobject in the array was set to null, so the loop was still iterating over the null! That's just a theory though, I'll go test out the new code, and tell you if it worked!
If you iterate until i
is equal to gameobjects.Length
(which you are doing according to that line), you will read beyond the array, which causes an access violation.
@christoph_r if you read the for loop he does some weird stuff when reaching the length of the array, he doesn't iterate on gameobjects beyond its length.
On top of that, accessing an array with an out of bounds index should only throw a managed IndexOutOfRangeException, certainly not result in an access violation or unity crashing.