Actually my code is very simple, i dontI don't understand why my object didnotdoes not follow the player. I instantiate the character with the PlayerPrefs I saved from another scene. However, the object tracking code does not follow, and also the referancereference is not Null.
Following Code:
using UnityEngine;
public class PlayerCamera : MonoBehaviour
{
public Transform handRef;
private void Start()
{
if (GameSceneCharAssignManager.instance == null || GameSceneCharAssignManager.instance.selectedChar == null)
{
Debug.LogError("Player or GameSceneCharAssignManager instance not set!");
return;
}
handRef = GameSceneCharAssignManager.instance.selectedChar.transform;
Debug.Log("Player assigned: " + handRef.name);
}
private void Update()
{
transform.position = handRef.position;
}
}
Referance'sReference Code:
public class GameSceneCharAssignManager : MonoBehaviour
{
public static GameSceneCharAssignManager instance;
[SerializeField] private GameObject[] playerPrefab;
[HideInInspector] public GameObject selectedChar;
private int selectedNo;
private void Awake()
{
instance = this;
selectedNo = PlayerPrefs.GetInt("SelectedPlayerIndex");
selectedChar = Instantiate(playerPrefab[selectedNo]);
}
}
```
using UnityEngine;
public class GameSceneCharAssignManager : MonoBehaviour
{
public static GameSceneCharAssignManager instance;
[SerializeField] private GameObject[] playerPrefab;
[HideInInspector] public GameObject selectedChar;
private int selectedNo;
private void Awake()
{
instance = this;
selectedNo = PlayerPrefs.GetInt("SelectedPlayerIndex");
selectedChar = Instantiate(playerPrefab[selectedNo]);
}
}