- Home /
I just tabbed at the wrong auto complete and its looking for the wrong component
How to assign the player transform to the Look At and Follow variables in a cinemachine through code?
Hello , I would like to get some help with an issue I am having for some reason, I don't know why.
When I initialize a player prefab I would like to make the cinemachine camera follow the player, and assign the look at and follow vars through code, this is the piece of code that should handle this on the player:
public void Start() {
if(hasAuthority) {
cam = GameObject.Find("MainCamera").transform; //This assigns the main camera to this variable I need to use somewhere else in the script, and this line works
GameObject go = GameObject.Find("ThirdPersonCamera");
InitiatePlayer other = (InitiatePlayer)go.GetComponent(typeof(InitiatePlayer));
other.AddPlayer(this.gameObject);
}
}
As you can see, I am referencing a script I made on the cinemachine called InitiatePlayer , it looks like this:
public class InitiatePlayer : MonoBehaviour { public void AddPlayer(GameObject player) {
//Right here
var vcam = GetComponent<CinemachineVirtualCamera>();
vcam.LookAt = player.transform;
vcam.Follow = player.transform;
}
}
Now, I did some debug logging to make sure the gameobject player was used in this latter function, at the place I marked with //Right here , I think this all should work and should assign the player transform to the Look At and Follow variables in the CinemachineFreeLook component, however it doen't assign and throws the following error: https://i.imgur.com/0EZCMqt.png
Can anyone provide some help? Ive been looking online already but couldnt find anything
Also, I think I provided enough info, if it isn't enough yet, of course just ask for more!
Thanks in advance :slight_smile: