Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.92 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class PlayerService : Singleton<PlayerService>
  6. {
  7.     public Dictionary<string, Player> Players;
  8.    
  9.     public void Start()
  10.     {
  11.         Players = GameObject
  12.             .FindGameObjectsWithTag("Player")
  13.             .ToDictionary(player => player.name, player => player.GetComponent<Player>());
  14.     }
  15. }
  16. public class Enemy : MonoBehaviour
  17. {    
  18.     void Update()
  19.     {
  20.         if(Input.GetKeyDown(KeyCode.Space))
  21.         {
  22.             PlayerService.Instance.Players["Player1"]?.DoSomething(this);
  23.         }
  24.         else if(Input.GetKeyDown(KeyCode.LeftShift))
  25.         {
  26.             PlayerService.Instance.Players["Player2"]?.DoSomething(this);
  27.         }
  28.     }
  29. }
  30.  
  31. public class Player : MonoBehaviour
  32. {
  33.     public void DoSomething(Enemy enemy)
  34.     {
  35.         Debug.Log($"{name} was told to do something by {enemy.name}");
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement