Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by masteproofgamel · Jun 09, 2016 at 05:15 PM · c#scripting problemgameobjectinstantiaterpg

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mmmpies · Jun 09, 2016 at 08:18 PM 0
Share

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FortisVenaliter · Jun 09, 2016 at 05:52 PM 1
Share

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.

avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image masteproofgamel · Jun 09, 2016 at 07:02 PM 0
Share

Can you please translate it in c# so I can understande better?

avatar image FortisVenaliter masteproofgamel · Jun 09, 2016 at 08:08 PM 0
Share

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.

avatar image masteproofgamel FortisVenaliter · Jun 09, 2016 at 08:56 PM 0
Share

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)
 }

?

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

180 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to instantiate a game object every time when space key is pressed 2 Answers

Destroyed instance of Prefab, can't spawn it back. 1 Answer

how to instantiate an object each time there is a certain amount of increase in the y position of another object? 1 Answer

I need help about Basic Basketball Game 0 Answers

I have 12 objects using the same script below, I need them to each activate individually of each other. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges