Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Confused · Dec 29, 2010 at 09:06 AM · booleantoggleplayersswitch characters

Switch Between Characters

I want to have a list of characters I will be able to switch between.

So far I am able to switch to "Player2" when I am currently "Player1" (when you press 1 you are player1, when you press 2 you are player2, etc).

I am able to do this by destroying my current character (Player1) and instantiating Player2 where I am standing (for now they spawn at a fixed point, but that I can change easily).

My problem is that when I press 2 on my keyboard a SECOND time (at this point I'm already playing as Player2), the GameObject Player2 is instantiated a SECOND time. If keep on doing this, I have a bunch of clones running around.

So to sum it up:

How do I stop the function being called by button1 after it is pressed until a different number is pressed? Or should I do a sort of toggle system? Or use a boolean of some sort? I tried these alternatives, but I am bombarded with errors. I am simply a beginner..

Here's the simple version of my script:

function Update(){

if (Input.GetKeyDown(KeyCode.Alpha1)){ print("1 pressed"); }

if (Input.GetKeyDown(KeyCode.Alpha2)){ print("2 pressed");
}

}


Here's what I'm really trying to work with...:


var Player1 : GameObject;

var Player2 : GameObject;

function Update(){

if (Input.GetKeyDown(KeyCode.E)){ print("E button!");

Destroy(GameObject.FindWithTag("Player1")); var instance1 : GameObject = Instantiate(Player2, transform.position, transform.rotation);

}

if (Input.GetKeyDown(KeyCode.R)){ print("R button!");

Destroy(GameObject.FindWithTag("Player2")); var instance2 : GameObject = Instantiate(Player1, transform.position, transform.rotation);

}

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Berenger · Dec 29, 2010 at 09:20 AM

The easiest will probably to have a variable to tell which player is instantiated.

var currentPlayer : int = 1;

function Update() { if (Input.GetKeyDown(KeyCode.Alpha1) && currentPlayer != 1 ){ print("1 pressed");
currentPlayer = 1; }

 if (Input.GetKeyDown(KeyCode.Alpha2) && currentPlayer != 2 ){
     print("2 pressed");    
     currentPlayer = 2; 
  }

}

Other idea, have tried to use player1.active = false; player2.active = true; instead ? (It need to be more clever than that, have a currentPlayer : GameObject; etc, but you get my point)

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
avatar image
0

Answer by drakedane · Jun 23, 2017 at 06:00 PM

Old discussion; but took me three days to work out "Character Switch" solution; so thought I would post my solution, in case it helps someone else. I am a beginner at coding; so it is possible my code could be more efficient; but it seems to work perfectly to switch between two characters. In my game, both players have a Main Camera and a Free Look Camera Rig. So those were some of the game objects that had to be switched. Here is the code I used...

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityStandardAssets.Characters.ThirdPerson;
 using UnityStandardAssets.Cameras;
 
 public class CharacerSwitch : MonoBehaviour
 {
     public GameObject player1;
     public GameObject player2;
 
     public GameObject cam3;
     public GameObject cam1;
 
     public GameObject rig1;
     public GameObject rig2;
 
     void Start()
     {
         player2 = GameObject.Find("Player2");
         player1 = GameObject.Find("Player1");
 
         cam3 = GameObject.Find("Camera3");
         cam1 = GameObject.Find("Camera1");
 
         rig2 = GameObject.Find("FreeLookCameraRig2");
         rig1 = GameObject.Find("FreeLookCameraRig");
 
         cam3.SetActive(false);
         rig2.SetActive(false);
 
         cam1.tag = "MainCamera";
         cam3.tag = "Camera 2";
     }
 
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.O))    //"O" for "Other" or "Other Player" 
         {
             if (player1.GetComponent<CapsuleCollider>().enabled == true)    //collider arbitrary, but reflects object state
             {
                 player1.GetComponent<Rigidbody>().isKinematic = true;
                 player2.GetComponent<Rigidbody>().isKinematic = false;
 
                 player1.GetComponent<CapsuleCollider>().enabled = false;
                 player2.GetComponent<CapsuleCollider>().enabled = true;
 
                 player1.GetComponent<ThirdPersonCharacter>().enabled = false;
                 player1.GetComponent<ThirdPersonUserControl>().enabled = false;
 
                 player2.GetComponent<ThirdPersonCharacter>().enabled = true;
                 player2.GetComponent<ThirdPersonUserControl>().enabled = true;
 
                 cam1.SetActive(false);
                 rig1.SetActive(false);
 
                 cam3.SetActive(true);
                 rig2.SetActive(true);
 
                 cam1.tag = "Camera 2";
                 cam3.tag = "MainCamera";
             }
         }
 
         if (Input.GetKeyDown(KeyCode.P))   //"P" for "Player" 
         {
             if (player1.GetComponent<CapsuleCollider>().enabled == false)   //collider arbitrary, but reflects object state
             {
                 player1.GetComponent<Rigidbody>().isKinematic = false;
                 player2.GetComponent<Rigidbody>().isKinematic = true;
 
                 player1.GetComponent<CapsuleCollider>().enabled = true;
                 player2.GetComponent<CapsuleCollider>().enabled = false;
 
                 player1.GetComponent<ThirdPersonCharacter>().enabled = true;
                 player1.GetComponent<ThirdPersonUserControl>().enabled = true;
 
                 player2.GetComponent<ThirdPersonCharacter>().enabled = false;
                 player2.GetComponent<ThirdPersonUserControl>().enabled = false;
 
                 cam1.SetActive(true);
                 rig1.SetActive(true);
 
                 cam3.tag = "Camera 2";
                 cam1 = GameObject.Find("Camera1");
                 cam1.tag = "MainCamera";
 
                 cam3.SetActive(false);
                 rig2.SetActive(false);
             }
         }
     }
 }
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 MagicalArtistJenniferSeeley · Feb 05, 2019 at 05:08 AM 0
Share

Thanks for the scripts. Going to try.

Only comment is....

public class CharacerSwitch : $$anonymous$$onoBehaviour

I think you forgot the "t" in the word "Character"....

~ :D ~

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

2 People are following this question.

avatar image avatar image

Related Questions

Toggle Boolean Function 2 Answers

Toggling bools automatically using coroutines 1 Answer

Boolean Toggle Cross-Script No Worky? 1 Answer

Change Variable on Raycast Hits something else? 1 Answer

Simplest way to attach toggles to booleans, maybe some kind of reference variable? 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