Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

coding trouble

Discussion in 'Scripting' started by moderatemaker, Jul 3, 2018.

  1. moderatemaker

    moderatemaker

    Joined:
    Jun 26, 2018
    Posts:
    3
    okay so i have this script, and when it reaches the 12 seconds into the game it doesn't actually switch to the players view point or activate the walking script, any ideas?


    Code (CSharp):
    1. public class Game1Camera : MonoBehaviour {
    2.     public GameObject PlayerCamera;
    3.     public GameObject CutSceneCamera1;
    4.     private GameObject player;
    5.     private float timeElapsed;
    6.  
    7.     void Start() {
    8.  
    9.         CutSceneCamera1.SetActive(true);
    10.         PlayerCamera.SetActive(false);
    11.  
    12.         GameObject varGameObject = GameObject.Find("player");
    13.         varGameObject.GetComponent<PlayerMove>().enabled = false;
    14.     }
    15.  
    16.      void Update() {
    17.             if (timeElapsed >= 12f)
    18.             {
    19.             PlayerCamera.SetActive(true);
    20.             CutSceneCamera1.SetActive(false);
    21.  
    22.             GameObject varGameObject = GameObject.Find("player");
    23.             varGameObject.GetComponent<PlayerMove>().enabled = true;
    24.             }
    25.         }
    26.     }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Well, a few things...first, keep in mind that once timeElapsed is >= 12f, it will continue to run the code inside the Update ever frame. Most likely, not what you want.

    Second, generally speaking, GameObject.Find is not a good idea to use, due to it being slow, but since I see you have a player variable that is private, not sure why you chose not to assign to it.

    But, the big one is...how are you incrementing timeElapsed? How does it become >= 12f if you aren't adding to it?
     
  3. moderatemaker

    moderatemaker

    Joined:
    Jun 26, 2018
    Posts:
    3
    okay so ive made these changes to the script and it still doesn't change

    Code (CSharp):
    1. public class Game1Camera : MonoBehaviour {
    2.     public GameObject PlayerCamera;
    3.     public GameObject CutSceneCamera1;
    4.     public GameObject player;
    5.     private float timeElapsed;
    6.  
    7.     void Start() {
    8.      
    9.         CutSceneCamera1.SetActive(true);
    10.         PlayerCamera.SetActive(false);
    11.      
    12.  
    13.         GameObject varGameObject = GameObject.Find("player");
    14.         varGameObject.GetComponent<PlayerMove>().enabled = false;
    15.     }
    16.  
    17.     private void Update()
    18.     {
    19.         timeElapsed += Time.deltaTime;
    20.     }
    21.  
    22.     void CameraChange() {
    23.             if (timeElapsed >= 12f)
    24.             {
    25.             PlayerCamera.SetActive(true);
    26.             CutSceneCamera1.SetActive(false);
    27.  
    28.             GameObject varGameObject = GameObject.Find("player");
    29.             varGameObject.GetComponent<PlayerMove>().enabled = true;
    30.             }
    31.         }
    32.     }
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    How is CameraChange being called?
     
  5. moderatemaker

    moderatemaker

    Joined:
    Jun 26, 2018
    Posts:
    3
    okay so i figured where i went wrong thanks for replying so quickly!