Search Unity

Assigning a GameObject to the Follow box via code?

Discussion in 'Cinemachine' started by DustyShinigami, Jul 8, 2019.

  1. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    After spending ages trying to get my player to survive scenes, keep his health and score in tact, and get my player in the right position between scenes, I'm now faced with yet another problem. Once my next scene has loaded, the object to follow for that scene's CM vcam is missing. Initially, I just dropped my prefab of the player into each scene, but due to certain circumstances, I can no longer do that, so I need my player to transfer between scenes using DontDestroyOnLoad.

    I don't suppose there's any way to drop the object I need into the Follow box via code on Start...? Is there any way to modify the script?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    Sure. vcam.Follow = bla
     
  3. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    Great. Though I'm confused on what I can put or how. I tried adding a public variable GameObject called thePlayer for a reference, but it doesn't work. It's all set up different to what I'm used to. :-\
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    The UX gives you a hint about what kind of object the field expects. In this case it's a Transform:

    upload_2019-7-11_8-10-55.png

    So you can do vcam.Follow = thePlayer.transform, for example.