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 /
  • Help Room /
avatar image
1
Question by stan1469 · Apr 27, 2014 at 10:37 PM · instantiatecharacterdestroyswitch

Switch between 2 Characters with one input

I have looked for hours in the unity forms and on YouTube but i can’t seem to figure out how to switch between 2 character with their own scripts i made prefabs of the two models with attached scripts in c# first i tried doing it with the script that spawns the player by instantiate the prefab and destroying the first one but doesn’t do anything i need a script and someone to explain this I am a noob im not sure what im missing but all help is appreciated //cant figure out how to post my code

Comment
Add comment · Show 4
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 Jeff-Kesselman · Apr 27, 2014 at 11:17 PM 0
Share

You need to explain better what you mean by "switch between 2 character with their own scripts"

Do yo humean switch which the camera is looking at? Do yo mean switch which is getting input?

avatar image KUFgoddess · Dec 13, 2016 at 08:01 PM 0
Share

Did you ever figure out how to do this? I'm in the same boat I need to switch between two characters ingame using the same input.

avatar image Datapax KUFgoddess · Dec 13, 2016 at 08:30 PM 0
Share

I'm not an expert but, you could make an empty game object, attach a "GameController" script which will be always instantiated. From that script you can monitor the input and Instantiate/Destroy the objects you wish :)

avatar image FlaSh-G · Jun 23, 2017 at 05:39 PM 0
Share

Please consider using some kind of punctuation in the future. This post is close to unreadable.

1 Reply

· Add your reply
  • Sort: 
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 · 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

25 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

Related Questions

Destroy prefabs after they have been instantiated (C#) 1 Answer

How do I destroy a GameObject/Script that instantiates other objects, without destroying the objects it has previously instantiated? 0 Answers

I need help with SyncVar. 0 Answers

destroying instantiated groups of objects 1 Answer

Unable to delete instance of GameObject 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