Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

help! how to reference 2 players in a scene instead of only one

Discussion in '2D' started by NomadRogers, Feb 9, 2020.

  1. NomadRogers

    NomadRogers

    Joined:
    Jan 14, 2020
    Posts:
    2
    Hi Team,

    So I have 2 players in my game, only one active at once and by a press of a button, they get swapped out for each other. At the moment my scripts are only really working with one player and not both. For example an enemy will chase 1 player but as soon as i change, he ignores the other. I am currently using "findobjectswithtag" in the start function. i understand having it occur in update is possible but that is very demanding. How else can I go about implementing this?

    Thanks
     
  2. maormenashe

    maormenashe

    Joined:
    Oct 7, 2018
    Posts:
    20
    At the same script you switch between the 2 players use a member that is a reference of the current active player.
    Give the enemy a reference to the same script and you'll easily be able to grab the active player from there.


    Code (CSharp):
    1. public class SwitchPlayersScript : MonoBehaviour
    2. {
    3.     public GameObject ActivePlayer;
    4.  
    5.     [SerializeField]
    6.     private GameObject _playerOne;
    7.  
    8.     [SerializeField]
    9.     private GameObject _playerTwo;
    10.  
    11.     private bool _isPlayerOneActive;
    12.  
    13.     private void Start()
    14.     {
    15.         ActivePlayer = _playerOne;
    16.         _isPlayerOneActive = true;
    17.     }
    18.  
    19.     private void Update()
    20.     {
    21.         if (Input.GetKeyDown("space"))
    22.         {
    23.             SwitchPlayers();
    24.         }
    25.     }
    26.  
    27.  
    28.     private void SwitchPlayers()
    29.     {
    30.         _playerOne.SetActive(!_isPlayerOneActive); // if _isPlayerOneActive = true deactivate playerOne
    31.         _playerTwo.SetActive(_isPlayerOneActive); // if _isPlayerOneActive = true activate playerTwo
    32.         _isPlayerOneActive = !_isPlayerOneActive;
    33.  
    34.         ActivePlayer = _isPlayerOneActive ? _playerOne : _playerTwo; // Set the newely activate player
    35.     }
    36. }
    37.    
    38. public class EnemyFollowScript : MonoBehaviour
    39. {
    40.     [SerializeField]
    41.     private SwitchPlayersScript _switchPlayersScript;
    42.  
    43.     private void Update()
    44.     {
    45.         FollowPlayer(_switchPlayersScript.ActivePlayer);
    46.     }
    47.  
    48.     private void FollowPlayer(GameObject activePlayer)
    49.     {
    50.         Debug.Log($"Follow {activePlayer.name}");
    51.     }
    52. }