- Home /
good way to get reference of transform of newly instantiated player object?
I'd like to get the Transform of a newly instantiated player object, to the gameManager script. so i made static unityevent on playerManager script (attached to player object) and it is being invoked when player is instantiated, but problem is that when player is instantiated for the first time, the static unityevent variable does not exist. is there any proper way to get the transform from player object, everytime it is instantiated? (the player object is being instantiated multiple times throughout the game. it has multiple game rounds like Among-Us)
public class PlayerManager : MonoBehaviour, IPunObservable {
public static PlayerManager singleton;
public static UnityEvent onLocalPlayerSpawnedEvent;
private void Awake() {
singleton = this;
pv = GetComponent<PhotonView>();
onLocalPlayerSpawnedEvent = new UnityEvent();
}
// Start is called before the first frame update
void Start() {
if (PhotonNetwork.connected) {
if (pv.isMine) {
OnLocalPlayerSpawn();
} else {
OnMultiPlayerSpawn();
}
} else {
OnLocalPlayerSpawn();
}
}
void OnLocalPlayerSpawn() {
onLocalPlayerSpawnedEvent.Invoke();
}
//Some other codes below...
public class GameManager: MonoBehaviour {
private Transform playerTransform;
private void Start() {
PlayerManager.onLocalPlayerSpawnedEvent.AddListener(GetPlayerTransform);
}
private void GetPlayerTransform() {
playerTransform = PlayerManager.singleton.transform;
} // some other codes below...
Your answer
Follow this Question
Related Questions
Singleton and multiple scene data 1 Answer
Referencing all objects in scene 2 Answers
trouble with referencing a componenet (bool) 1 Answer
Instantiated Objects dont get their Script referenced properly 2 Answers
why doesn't my reference work? 1 Answer