Search Unity

How do I set height of a camera/gameobject from another scene

Discussion in 'Scripting' started by Tezelian, Nov 5, 2018.

  1. Tezelian

    Tezelian

    Joined:
    Oct 22, 2013
    Posts:
    256
    Hey guys,

    I want to give the player a choice to select the height of the VR camera game object from the menu scene based on the button they click

    How do I pass/alter variables from one scene to another?

    The camera game-object only exists in scene called Main but I want the player to be able to change the position based on what they choose in Main-Menu scene. Alternatively I could just do each button connecting to separate scenes with the heights I want but with a script that just changes the height of the game-object

    On the menu I have this code attached to a button for an OnClickfunction

    Code (CSharp):
    1. public void Tall()
    2.  
    3. {
    4.  
    5. PlayerPrefs.SetFloat("HeightTall", 10.38f);
    6.  
    7. Debug.Log(PlayerPrefs.GetFloat("HeightTall").ToString());
    8.  
    9.  
    10. //SceneManager.LoadScene("Main");
    11.  
    12. }

    Once it goes into the next scene I have this line of code attached to the maincamera


    Code (CSharp):
    1. public GameObject MyVrCam;
    2.  
    3. public void Start()
    4.  
    5. {
    6.  
    7. PlayerPrefs.GetFloat("HeightTall");
    8.  
    9. MyVrCam.transform.position = new Vector3(0.0f, HeightTall, 0.0f);
    10.  
    11. }
    12.  
    13. }

    I basically want the 'HeightTall' to be the new y position declared in the menu scene but that is being underlined as an error.
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536