Instantiate prefab with UCC scripts

noakwollsater

New member
So I wanted to try something and got stuck big time..
I did a character creation scene where you can change hair etc on your character.
The prefab has already gone trough the UCC manager with all the scripts etc..

But when the user is done with the character and presses create and the instantiate starts the new made prefab dont save all the script from UCC correct. Like its missing abilitys and the Unity Input is missing etc .. what am i doing wrong?
 
Can you point out the time in the video that shows the differences? Also, what are you doing when the user presses the create button? Are you instantiating the same prefab that you show for the character selection?
 
Can you point out the time in the video that shows the differences? Also, what are you doing when the user presses the create button? Are you instantiating the same prefab that you show for the character selection?
Like the ModularCharacterInput is gone after the Instantiation. And som settings inside some of the UCC scripts like the Ultimate Character Locomotion is not set up correct.

But like in the video you can se the model in the hierarchy(also the model the gets instantiated) he has this Input object with the Unity Input attached, Also i go trough the Character Locomotion script. Then after the instantiate I go check the new prefab who's missing the input object with the unity input script, and settings of the Ultimate Character Locomotion script is not the same.
 
The PlayerInput gets moved to under the Game GameObject. This prevents input from being disabled if the character GameObject is disabled.

When you instantiate an object it will use the same settings as what you have in your prefab. If you made changes at runtime those settings will not persist.
 
The PlayerInput gets moved to under the Game GameObject. This prevents input from being disabled if the character GameObject is disabled.

When you instantiate an object it will use the same settings as what you have in your prefab. If you made changes at runtime those settings will not persist.
Yeah I noticed that the playerinput gets moved to another place, but when i Instantiate it the input dont follow. Should i maybe make a script that adds the input again to the new prefab?
 
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.
 
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.");
}
}
 
Please use code tags so it's easier to read. With that said, I'm not sure what GetPlayerModel is returning so it's tough to say. Regardless you should be using the same project-level prefab that contains all of the references. If you want to instantiate a character from scratch you'll need to use the CharacterBuilder - take a look at the UMA integration for an example.
 
Back
Top