Hmm, how are you instantiating your prefab? You should be using the project-level prefab and not an already-instantiated GameObject. All settings don't get copied when you do that.
If you are instantiating a new character that has the PlayerInput component you should remove the original PlayerInput component since that one is no longer valid. You could also reassign but removing is probably easier since you are going to have to duplicate PlayerInputs anyway.
public void NewPrefabandMaterial()
{
Debug.Log("NewPrefabandMaterial called");
// Enable all character scripts
EnableAllCharacterScripts();
// Ensure the username is available from SessionManager
string username = SessionManager.Instance.Username;
if (string.IsNullOrEmpty(username))
{
Debug.LogError("Username is not available in SessionManager.");
return;
}
// Set the path to the user's directory
string path = "Assets/Users/" + username;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
// Creates folder inside asset map with username as name
string characternamepath = "Assets/Users/" + username + "/" + player.playerNameInput.text;
if (!System.IO.Directory.Exists(characternamepath))
{
System.IO.Directory.CreateDirectory(characternamepath);
}
// Copy the shader from the original material
Shader originalShader = characterMaterial.shader;
// Create a new material instance with the same shader
Material newMaterial = new Material(originalShader)
{
name = player.playerNameInput.text + "_PlayerMaterial"
};
// Copy all the properties from the original material
newMaterial.CopyPropertiesFromMaterial(characterMaterial);
// Save the material as an asset
#if UNITY_EDITOR
string materialPath = Path.Combine(characternamepath, newMaterial.name + ".mat");
UnityEditor.AssetDatabase.CreateAsset(newMaterial, materialPath);
UnityEditor.AssetDatabase.SaveAssets();
#endif
// Create a new prefab from the current player model
GameObject playerModel = GetPlayerModel();
if (playerModel != null)
{
// Instantiate the full hierarchy of the model
GameObject newPrefabInstance = Instantiate(playerModel);
newPrefabInstance.name = player.playerNameInput.text + " " + player.playerLastnameInput.text + "_" + playerModel.name;
// Set the scale of the prefab to 1,1,1
newPrefabInstance.transform.localScale = Vector3.one;
// Ensure all renderers have the new material
Renderer[] renderers = newPrefabInstance.GetComponentsInChildren<Renderer>();
foreach (Renderer renderer in renderers)
{
Material[] materials = renderer.sharedMaterials;
for (int i = 0; i < materials.Length; i++)
{
if (materials
.name == characterMaterial.name)
{
materials = newMaterial;
}
}
renderer.sharedMaterials = materials;
}
#if UNITY_EDITOR
// Save the skill information in a text file
SaveSkillInformation(characternamepath, newPrefabInstance.name);
// Save the new prefab in the user's folder
string prefabPath = Path.Combine(characternamepath, newPrefabInstance.name + ".prefab");
UnityEditor.PrefabUtility.SaveAsPrefabAsset(newPrefabInstance, prefabPath);
// Store the prefab name in PlayerPrefs or another persistent storage
PlayerPrefs.SetString("LastCreatedPrefab", newPrefabInstance.name);
// Destroy the instantiated prefab after saving
DestroyImmediate(newPrefabInstance);
#endif
}
else
{
Debug.LogError("Player model is not set in PlayerData.");
}
}