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
1
Question by Dan_Tsukasa · Jun 02, 2013 at 02:03 AM · javascriptcharacterdisableenableactive

Set Player 2 to use player 1's postion in world

Hey Guys

What I'm trying to do is, using this rather messy code, when I press say 2, to switch from Character 1 to Character 2, I want Player 2 to have the same Position as Player 1, I've tried several methods so far but nothing seems to be working correctly.

This is the code I have running so far (I'm sure my way of turning on and off Characters is a terrible way, but for the moment it works until I can think of a more elegant and smart solution).

I'd appreciate any help or tips you can provide, I'm sure its a small thing but It's just not getting there. Thanks guys.

     var currentPlayer : int = 1;
     var C1 : GameObject;
     var C2 : GameObject;
     var C3 : GameObject;
      
     function Update()
     {
     if (Input.GetKeyDown(KeyCode.Alpha1) && currentPlayer != 1 ){
     print("1 pressed");
     C1.SetActive(true);
     C2.SetActive(false);
     C3.SetActive(false);
     }
      
     if (Input.GetKeyDown(KeyCode.Alpha2) && currentPlayer != 2 ){
     print("2 pressed");
     C1.SetActive(false);
     C2.SetActive(true);
     C3.SetActive(false);
     }
     
     if (Input.GetKeyDown(KeyCode.Alpha3) && currentPlayer != 3 ){
     print("3 pressed");
     C1.SetActive(false);
     C2.SetActive(false);
     C3.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
0

Answer by aldonaletto · Jun 02, 2013 at 02:40 AM

If you want to move the newly selected character to the current character position, it would be better to store the characters in an array - this would also reduce a lot your code:

 var currentPlayer : int = 0; // player numbers start at 0 now
 var players: GameObject[]; // set the size and assign the players in the Inspector

 function Update()
 {
   var selPlayer = currentPlayer; // assume no change
   // verify if a new player is being selected:
   if (Input.GetKeyDown(KeyCode.Alpha1)) selPlayer = 0;
   if (Input.GetKeyDown(KeyCode.Alpha2)) selPlayer = 1;
   if (Input.GetKeyDown(KeyCode.Alpha3)) selPlayer = 2;
   if (selPlayer != currentPlayer){ // if player changed...
     players[selPlayer].SetActive(true); // activate new player
     // move new player to current position:
     players[selPlayer].transform.position = players[currentPlayer].transform.position;
     players[currentPlayer].SetActive(false); // deactivate old player
     currentPlayer = selPlayer; // update variable currentPlayer
   }
 }
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 Dan_Tsukasa · Jun 02, 2013 at 06:53 PM 0
Share

Oh Wow great thanks, you answered my question and fixed my code too, couldn't have asked for a better answer, thankyou so much.

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

15 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

Related Questions

How To Keep GameObjects From Enabling Again? 2 Answers

If you disable a gameobject, does an InvokeRepeating loop end or pause? 3 Answers

GUIText enable/disable 2 Answers

Recommended way to delay controls momentarily? 2 Answers

enable/disable image effects in code 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