Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How do i change Cinemachine's LookAt and Follow from a script?

Discussion in 'Scripting' started by Jonhodisme, May 13, 2020.

  1. Jonhodisme

    Jonhodisme

    Joined:
    May 13, 2020
    Posts:
    1
    So, first of all, i am pretty new to unity, and I am trying to create a multiplayer game using photon.
    I have a script that spawns two players at the start of the scene, and I need to use them as the LookAt and Follow, but I keep getting back this error:

    NullReferenceException: Object reference not set to an instance of an object
    it refers to the spawning script:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using Photon.Pun;
    5. using UnityEngine;
    6. using Cinemachine;
    7.  
    8. public class PlayerSpawner : MonoBehaviour
    9. {
    10.     [SerializeField] private GameObject playerPrefab = null;
    11.     [SerializeField] private CinemachineFreeLook playerCamera = null;
    12.  
    13.     private void Start()
    14.     {
    15. //spawn player
    16.         var player = PhotonNetwork.Instantiate(playerPrefab.name, Vector3.zero, Quaternion.identity);
    17.  
    18. //connect Cinemachine
    19.         playerCamera.Follow = player.transform;
    20.         playerCamera.LookAt = player.transform;
    21.     }
    22. }
    23.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    I am developing a new response to this most-common question:

    http://plbm.com/?p=221

    Feedback and comments to that post are always welcome.
     
    PraetorBlue likes this.
  3. Hochoy

    Hochoy

    Joined:
    Jul 26, 2019
    Posts:
    1
    you dont have a 'GameObject' assigned to 'playerPrefab'