RPG instantiate problem
So I made a character class creation but I have a problem about instatiating the player in a new scene. The script is set like this:
if (isWarriorClass == true || isMageClass == true || isArcherClass == true)
{
SceneManager.LoadScene("scene_main");
Instantiate(newPlayer.Model);
}
But it loads the scene and it doesn't instantiate the public GameObject. This is wrote in a public function that is called when a "Create player" button is clicked. Can someone tell me how can I instantiate a GameObject after a scene is loaded?
You need to understand how Unity handles data between scenes. All your characters settings will be lost when you move between scenes unless you understand data persistence. This is a really great tutorial on it...
https://unity3d.com/learn/tutorials/topics/scripting/persistence-saving-and-loading-data
Answer by QuantumMaster · Jun 09, 2016 at 05:24 PM
The function "Instantiate" takes three arguments: The original prefab Location to put it Rotation of object
The script first has to know which object to use, which you can do by having a public GameObject variable.
public GameObject newPlayer
You have to drag and drop your player model onto the empty GameObject slot in the script. Then you can instantiate your prefab.
Instantiate(newPlayer, new Vector3(x, y, z), Quaternion.Euler(x, y, z));
The Vector3 is the x, y, and z position. The Quaternion.Euler is the x, y, and z rotation.
I hope this helps!
No, one of the instantiate functions takes three arguments. I usually use the single argument version, like @masteproofgamel, because I prefer to set those values manually afterwards. Either use case is correct.
Answer by FortisVenaliter · Jun 09, 2016 at 05:50 PM
The problem is that LoadScene doesn't happen immediately. It may take a frame. When you run that code, it's likely instantiating it before the new scene is loaded. So, the best way to do this would be to have a spawner object in the new scene that takes the data from the old scene, and spawns the player in it's Start function.
Can you please translate it in c# so I can understande better?
Well, I basically described the algorithm for you. If you understand C# and Unity, it should be pretty trivial to write it. But I can't do it for you because I don't know how your game is set up.
Can you tell me how can I get the bool from another script to another? So for example, in the new scene I have a spawn gameobject with a spawn script. In this spawn script i should set something like
private Script_Name pCreation;
if(pCreation.isWarriorClass == true)
{
Instantiate(warriorCharacter)
}
if(pCreation.is$$anonymous$$aageClass == true)
{
Instantiate(mageCharacter)
}
if(pCreation.isArcherClass == true)
{
Instantiate(arhcerCharacter)
}
?
Answer by Jessespike · Jun 09, 2016 at 07:48 PM
Instantiate is not working, because the next scene is loaded just before. Move the Instantiate to a Start function in the next scene.