Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
avatar image
0
Question by ImmerAmPushen · Feb 24, 2020 at 07:25 PM · buttonsbutton trigger events

Chraracter Selection

Hello,

I am trying to do a Character Selection System for my game. Currently, I have 2 Scenes, a Map-scene and a ChooseCharacter-scene. I am using 4 buttons (1 button for each character) with an Event Trigger (Pointer Click) and I have GameController with 2 Scripts. My problem is that the selected character is still spawning on the ChooseCharacter-scene and not on the map-scene.

The GameController: alt text

Policeman button: alt text

The Spawncharacterscript:

 public class CharacterController : MonoBehaviour
 {
     public GameObject[] Players;
 
     public void OnClickedOne(Button button)
     {
         SceneManager.LoadScene(1); // Load Scene 1
         // yield return new WaitForSeconds(5); didn´t work
         Vector3 pos = new Vector3(0.0f, 1.0f, 0.0f); // coords of the Player
         GameObject player = Instantiate(Players[0], pos, Quaternion.identity) as GameObject; // Instantiate the first Character
     }
 
     public void OnClickedTwo(Button button)
     {
         SceneManager.LoadScene(1); // Load Scene 1
         Vector3 pos = new Vector3(0.0f, 1.0f, 0.0f); // coords of the Player
         GameObject player = Instantiate(Players[1], pos, Quaternion.identity) as GameObject; // Instantiate the second Character
     }
 
     public void OnClickedThree(Button button)
     {
         SceneManager.LoadScene(1); // Load Scene 1
         Vector3 pos = new Vector3(0.0f, 1.0f, 0.0f); // coords of the Player
         GameObject player = Instantiate(Players[2], pos, Quaternion.identity) as GameObject; // Instantiate the third Character
     }
 
     public void OnClickedFour(Button button)
     {
         SceneManager.LoadScene(1); // Load Scene 1
         Vector3 pos = new Vector3(0.0f, 1.0f, 0.0f); // coords of the Player
         GameObject player = Instantiate(Players[3], pos, Quaternion.identity) as GameObject; // Instantiate the fourth Character
     }
 }

The Scenecontroller:

 public class SceneController : MonoBehaviour
 {
     public void OnMouseClick()
     {
         SceneManager.LoadScene(1);
     }
 }
gamecontroller.png (22.8 kB)
button.png (16.1 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by unity_ek98vnTRplGj8Q · Feb 24, 2020 at 10:13 PM

Your character controller exists in the character selection scene, so anything in it will spawn in the character selection scene. I recommend either spawning your character before you call LoadScene() and then calling DontDestroyOnLoad() to make it persist into the next scene, or at least setting some variables in a persistent script so that the character can be loaded in the new scene by a script that exists in that scene. This can be accomplished either by setting some static variable values or by making a component instance (like a characterSpawnManager class or something) that you call DontDestroyOnLoad on.


A couple other notes - You call LoadScene from two different scripts. Are these both called when a button is clicked? This will likely cause the scene to load twice. Also, unless you are trying to load multiple scenes at once, don't put code after a LoadScene call

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 ImmerAmPushen · Feb 25, 2020 at 04:16 PM 0
Share

I tried it with the DontDestroyOnLoad, but it did not work. I also tried to make a GameObject with a Script that Instantiates every Character and after the character selection the other three characters should despawn, but this didn´t work as well. Yes, I was accidentally calling the LoadScene two times... Thanks for the tip :) Do you know any other option I could try? Tanks in advance!

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

123 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

Related Questions

How do I remove action listeners from a button without actually clicking the button? 0 Answers

Creating a Jeopardy style game, script's not working. 0 Answers

Door won't go into button slot,My door mesh won't be put into the GameObject script. 0 Answers

UI Buttons Not Working in Dual Screen Setup 0 Answers

cant run function when button pressed 1 Answer


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