I don't understand why my object does 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 reference 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;
}
}
Reference Code:
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]);
}
}
transform.position = handRef.position;to check what object ishandRef? How to use the debugger of your code editor of choice is a topic most YouTube tutorial creators don't explain. Because the moment they do, they lose most of their audience, because with knowledge of how to use the debugger, people quickly become self-sufficient enough to no longer need tutorials. \$\endgroup\$