Hey guys I’ve a problem. The reference of the camera to the player gets lost when i start the game because the Player gets cloned. If you can pls help me.
Thank you! :=)
Hey guys I’ve a problem. The reference of the camera to the player gets lost when i start the game because the Player gets cloned. If you can pls help me.
Thank you! :=)
Depending on the need, you either “setup” the player when you clone it and attach the camera at this point. Or you use a manager script that holds a reference to the camera and the player simply accesses the camera through that manager script instead of holding it’s own reference to the camera.
Okay thank you, but how can i do it?
Fast and dirty way is to tag the player “Player” and then just find the player with GameObject.FindGameObjectWithTag(“Player”) in your camera script in Start()
Hey guys, now here is the code which i use. I still got the same problem the camera is not attached to the player.
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
public class followplayer : MonoBehaviour
{
public Transform player;
public Vector3 offset;
// Update is called once per frame
void Start()
{
GameObject.FindGameObjectWithTag("Player");
}
void Update()
{
transform.position = player.position + offset;
}
}
You need to actually assign your player variable:
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
}