I don't know if this is even possible but, I'm trying to add an array/list of Components to a prefab made in a function; Code (CSharp): void CreatePrefab(string prefab_name, Component[] toAdd) { GameObject newPrefab = new GameObject (); newPrefab.name = prefab_name; foreach ( Component dAdd in toAdd ) { newPrefab.AddComponent<dAdd>(); } } I of course get a "nameSpace" dAdd could not be found. Am I a madman for trying to do all this?
The method: AddComponent<T>() takes in a type for T and has to be a known type in that scope. It then creates a NEW component based off that type. There is another version that takes a System.Type object as its parameter. The array 'toAdd' is a collection of components, not types. Even if it were types, you can't use the generics version of the function, but instead the version that takes the type object as a parameter. Either way, you can't add existing components to a new GameObject. You have to create a NEW component on that GameObject. In this case you can do that by saying on line 9: Code (csharp): newPrefab.AddComponent(dAdd.GetType());
Thanks, this was a huge help. I guess I wasn't too far off at least? Just didn't know to add .GetType() . Again, this makes a huge difference. Thanks.