0
\$\begingroup\$

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]);
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ Can you show us your console output when running this code after a scene transition? \$\endgroup\$ Commented Jun 4 at 12:25
  • \$\begingroup\$ Do you get the "Player assigned" message in the console? \$\endgroup\$ Commented Jun 5 at 7:27
  • \$\begingroup\$ Have you tried to set a debugger breakpoint in transform.position = handRef.position; to check what object is handRef? 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\$ Commented Jun 5 at 9:20

1 Answer 1

-2
\$\begingroup\$

I can't say for sure that this will solve the issue, but I think you may want handRef to reference selectedChar, not selectedChar.transform. handRef will be outdated if selectedChar changes- I can't say whether that's happening since there isn't enough information in the post, but it could be a bug either way. If this doesn't solve your problem, please post any output such as the console log and explain the actual and expected behavior in more detail. Are there players? Is there anything relevant in the player class?

\$\endgroup\$
3
  • 1
    \$\begingroup\$ I don't see a reason why the code above should access the transform indirectly through a GameObject reference instead of directly. \$\endgroup\$ Commented Jun 5 at 7:26
  • \$\begingroup\$ Changes to the selectedChar won't propagate to handRef and there's not enough information to give a more specific/accurate answer. Just based on this code, if selectedChar changes then our transform will be outdated, and it's possible that selectedChar is changed before the camera gets to move. Again, I think more information is needed, but I think using the GameObject reference makes more sense for what the op is trying to accomplish. \$\endgroup\$ Commented Jun 5 at 8:44
  • \$\begingroup\$ I'm not sure if this should've been a comment or an answer to be honest, but I don't have enough reputation to comment x x \$\endgroup\$ Commented Jun 5 at 9:00

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.