Resources.LoadAll in scriptable Wizard loads each resource twice

Hello,

I’ve a strange problem : I’m writing a wizard which shall construct a ski track out of segment prefabs. To load the prefabs, I’m using Resources.LoadAll, but the problem is that this method seems to load all prefabs twice. Furthermore, some of them can not be cast into GameObjects.

Here the C# script boiled down to what shows the problem :

class TestWizard : ScriptableWizard 
{
	[MenuItem ("Creative Patterns/Test Wizard")]
	static void CreateWizard () 
	{
		ScriptableWizard.DisplayWizard<TestWizard>("Test Wizard", "Test" );
	}
	
	void OnWizardCreate () 
	{
		 int i ;
		Object[] aPrefabs =Resources.LoadAll("Track");	
		for( i = 0; i < aPrefabs.Length; i++ )
		{
			Debug.Log( "Loaded "+aPrefabs[i].name+" for i = "+i);
			GameObject gameobject = aPrefabs[i] as GameObject;
			if( gameobject == null )
			{
				Debug.Log("gameobject == null ");
			}
		}		
		Debug.Log( "aPrefabs.Length =  "+aPrefabs.Length );
	}
}

Here a screenshot which shows on the left the project view with the Assets folder, and on the right the output of said script on the console. As you can see, I have 12 prefabs but each one is loaded twice.

Where does this come from ? Is it a bug in my code or a Unity bug ? (Unity doesn’t load the .meta files as separate resources, does it ?)

I’m not entirely sure why it is designed like this but Resources.LoadAll returns the object and its transform as two separate references. Resources.LoadAll has an optional second parameter that filters the types of object returned by the function - if you pass the GameObject type as the filter then you should find the prefabs are only returned once and they are always GameObjects.

1 Like