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 /
avatar image
0
Question by FairySeraph · Mar 29, 2018 at 03:26 AM · switchswitchingswitch characters

Error With Switching Characters

So, I have this script to change characters in-game, like in Castlevania: Portrait Of Ruin, for example. It seems to work with the switching part, but when the characters change, they don't stick together. It's like this: say I move to a corner of a room. Then, I change characters and move to the other corner. Now, when I switch back, the first character appears in the previous corner, as if the game saved where it was located when the switch was made. How can I get them to stay together when I switch?

Regarding my setup: I have two characters inside a parent empty gameobject. The characters themselves have the controls script, rigidbodies, colliders, etc, and the empty gameobject has the switching script. This was the only way I could find to get it to work with the character animations. Here is the switching script:

 public GameObject play1, play2;
 private int characterSelect;


 void Start()
 {
     characterSelect = 1;
     play1 = GameObject.Find("Player1");
     play2 = GameObject.Find("Player2");

 }


 void FixedUpdate()
 {
     if (Input.GetButtonDown("Change"))
     {
         if (characterSelect == 1)
         {
             characterSelect = 2;
         }
         else if (characterSelect == 2)
         {
             characterSelect = 1;
         }
     }
     if (characterSelect == 1)
     {
         play1.SetActive(true);
         play2.SetActive(false);
     }
     else if (characterSelect == 2)
     {
         play1.SetActive(false);
         play2.SetActive(true);
     }
 }
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
1
Best Answer

Answer by spencersteers · Mar 29, 2018 at 11:23 AM

Once you disable a GameObject all the attached components will be disabled. Even though you have your control script on both objects it will only be active on the active GameObject.

A quick way to get the characters in the same position would be to update the deactivated players transform just before switching characters.

     if (characterSelect == 1)
      {
          play1.transform.position = play2.transform.position;
          play1.transform.rotation = play2.transform.rotation;
          play1.SetActive(true);
          play2.SetActive(false);
      }
      else if (characterSelect == 2)
      {
          play2.transform.position = play1.transform.position;
          play2.transform.rotation = play1.transform.rotation;
          play1.SetActive(false);
          play2.SetActive(true);
      }

That should fix the problem you are having now.

Looking into the future though you might run into similar issues like this. Ignore me if I am wrong but I'm guessing you have the same Control script on two separate GameObjects? Instead of activating / deactivating a multiple players I would recommend just enabling / disabling the visual game objects when you switch characters so that you only have 1 instance of your Control script.

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 FairySeraph · Mar 30, 2018 at 12:12 AM 0
Share

Nah, this just locks the players into place and makes them sink into the floor. Can't move. But, yeah, they both have the same control script. I thought I was already activating only one at a time with the "SetActive(True/False)" thing.

avatar image spencersteers FairySeraph · Mar 30, 2018 at 12:36 AM 1
Share

Oh my bad I forgot that was inside FixedUpdate. Try moving the if else inside if (Input.GetButtonDown("Change"))

avatar image FairySeraph spencersteers · Mar 30, 2018 at 05:13 AM 0
Share

I got it to work! Here's the finished code:

 public GameObject play1, play2;
 private int characterSelect;

 void Start()
 {
     characterSelect = 1;
     play1 = GameObject.Find("Player1");
     play2 = GameObject.Find("Player2");

 }


 void Update()
 {
     if (Input.GetButtonDown("Change"))
     {
         if (characterSelect == 1)
         {
             characterSelect = 2;
         }
         else if (characterSelect == 2)
         {
             characterSelect = 1;
         }


     }

     if (characterSelect == 1)
     {
         play1.SetActive(true);
         play2.SetActive(false);
         play2.transform.position = play1.transform.position;
         play2.transform.rotation = play1.transform.rotation;
     }
     else if (characterSelect == 2)
     {
         play1.SetActive(false);
         play2.SetActive(true);
         play1.transform.position = play2.transform.position;
         play1.transform.rotation = play2.transform.rotation;
     }

 }

I think I'm going to add a cooldown timer in between changes sometime later, though. I noticed that if I rapidly switch between characters, I can sort of "glide" in mid jump and go further than a normal jump allows. Not exactly an exploit I want in my game ;) Anyway, thanks, man!

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

76 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

Related Questions

changing player (getting in vehicles) 3 Answers

Switching cameras (JavaScript) 0 Answers

Developing a Universal Unity Game and tips. 0 Answers

switching between in game characters 2 Answers

Changing player model with keypress. 3 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