Skip to main content
This is C# code, not the deprecated language UnityScript. Please be sure to read tag descriptions.
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

Why isn't my object isnt following the target?

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]);
    }
}

Why my object isnt following the target?

Actually my code is very simple, i dont understand why my object didnot 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 referance 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's 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]);
    }
}
```

Why isn't my object following the target?

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]);
    }
}
Source Link

Why my object isnt following the target?

Actually my code is very simple, i dont understand why my object didnot 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 referance 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's 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]);
    }
}
```