Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- using System.Linq;
- public class PlayerService : Singleton<PlayerService>
- {
- public Dictionary<string, Player> Players;
- public void Start()
- {
- Players = GameObject
- .FindGameObjectsWithTag("Player")
- .ToDictionary(player => player.name, player => player.GetComponent<Player>());
- }
- }
- public class Enemy : MonoBehaviour
- {
- void Update()
- {
- if(Input.GetKeyDown(KeyCode.Space))
- {
- PlayerService.Instance.Players["Player1"]?.DoSomething(this);
- }
- else if(Input.GetKeyDown(KeyCode.LeftShift))
- {
- PlayerService.Instance.Players["Player2"]?.DoSomething(this);
- }
- }
- }
- public class Player : MonoBehaviour
- {
- public void DoSomething(Enemy enemy)
- {
- Debug.Log($"{name} was told to do something by {enemy.name}");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement