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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

how do i change cinemachine follow transform in script?

Discussion in 'Cinemachine' started by secxhs, Apr 26, 2021.

  1. secxhs

    secxhs

    Joined:
    Apr 26, 2021
    Posts:
    6
    How can i? Can u send me example script?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,238
    vcam.Follow = myNewTarget
     
  3. secxhs

    secxhs

    Joined:
    Apr 26, 2021
    Posts:
    6
    I'll try as soon, thanks.
     
  4. secxhs

    secxhs

    Joined:
    Apr 26, 2021
    Posts:
    6
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using EZCameraShake;
    5. using Cinemachine;
    6.  
    7. public class SwitchCharacter : MonoBehaviour
    8. {
    9.  
    10.     public GameObject Player, Ship, CameraHandler;
    11.  
    12.     private CinemachineVirtualCamera vcam;
    13.  
    14.     void Start()
    15.     {
    16.         vcam = GetComponent<CinemachineVirtualCamera>();
    17.  
    18.         Player.gameObject.SetActive(true);
    19.         Ship.gameObject.SetActive(false);
    20.     }
    21.     void OnTriggerEnter2D(Collider2D coll)
    22.     {
    23.         if (coll.gameObject.CompareTag("TransformPortalForPlayer"))
    24.         {
    25.             Ship.transform.localPosition = Player.transform.localPosition;
    26.             vcam.Follow = Ship.transform;
    27.             //CameraHandler.GetComponent<CameraMovement>().trg = Ship.transform;
    28.             Player.gameObject.SetActive(false);
    29.             Ship.gameObject.SetActive(true);
    30.             Destroy(coll.gameObject);
    31.             CameraShaker.Instance.ShakeOnce(5f, 5f, .1f, 1f);
    32.         }
    33.         else if (coll.gameObject.CompareTag("TransformPortalForShip"))
    34.         {
    35.             Player.transform.localPosition = Ship.transform.localPosition;
    36.             vcam.Follow = Player.transform;
    37.             //CameraHandler.GetComponent<CameraMovement>().trg = Player.transform;
    38.             Ship.gameObject.SetActive(false);
    39.             Player.gameObject.SetActive(true);
    40.             Destroy(coll.gameObject);
    41.             CameraShaker.Instance.ShakeOnce(5f, 5f, .1f, 1f);
    42.         }
    43.     }
    44. }
    NullReferenceException: Object reference not set to an instance of an object
    SwitchCharacter.OnTriggerEnter2D (UnityEngine.Collider2D coll) (at Assets/Scripts/SwitchCharacter.cs:27)

    I get this error, why? Thanks again.
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,238
    this line
    Code (CSharp):
    1. vcam = GetComponent<CinemachineVirtualCamera>();
    is assuming that this script is attached to the vcam. Is it?
     
  6. secxhs

    secxhs

    Joined:
    Apr 26, 2021
    Posts:
    6
    Oh, no. I'll write again and make a return
     
  7. secxhs

    secxhs

    Joined:
    Apr 26, 2021
    Posts:
    6
    it worked, thank you
     
    Gregoryl likes this.