41 lines
1.0 KiB
C#
41 lines
1.0 KiB
C#
using System.Linq;
|
|
using UnityEngine;
|
|
|
|
public class PlayerSpawnController : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private GameObject _player;
|
|
[SerializeField]
|
|
private GameObject _playerPrefab;
|
|
[SerializeField]
|
|
private GameObject _spawnLocation;
|
|
|
|
|
|
private void Awake()
|
|
{
|
|
var otherSpawnControllers = FindObjectsOfType<PlayerSpawnController>().Except(new[] { this });
|
|
if (otherSpawnControllers.Any())
|
|
Destroy(this);
|
|
else
|
|
DontDestroyOnLoad(this.gameObject);
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (GameObject.FindGameObjectWithTag("Player") != null)
|
|
{
|
|
_player = GameObject.FindGameObjectWithTag("Player");
|
|
_player.transform.position = _spawnLocation.transform.position;
|
|
_player.transform.rotation = _spawnLocation.transform.rotation;
|
|
|
|
}
|
|
else
|
|
{
|
|
_player = Instantiate(_playerPrefab);
|
|
_player.transform.position = _spawnLocation.transform.position;
|
|
_player.transform.rotation = _spawnLocation.transform.rotation;
|
|
DontDestroyOnLoad(_player);
|
|
}
|
|
}
|
|
|
|
} |