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

Question camera follow instantiated Prefab?

Discussion in 'Scripting' started by riasillo, Sep 11, 2023.

  1. riasillo

    riasillo

    Joined:
    May 23, 2023
    Posts:
    25
    I have a camera controller that was working with everything in hierarchy, with a serialize field I could drag ball into so camera could follow... but I created a game manager (following Imphenzia tutorrial,) loading a ballPrefab with new levels and deleting ball and level out of hierarchy.

    everything loads fine, it's just the camera move obviously...

    I just don't know coding to know how to hard code the followtarget definition to follow the instantiated ball prefab

    assuming it's some sort of void update defining followTarget = to ballPrefab (or possibly _currentBall) calling on it in gameManger...
    I just don't know how to write it properly...any help much appreciated.

    snippets of relevant code below.
    camera controller info.
    Code (CSharp):
    1. [SerializeField] Transform followTarget;


    gameManager info: ps. I tried to BOLD the relevant parts of code, but it put in B in brackets that's not in my code...
    Code (CSharp):
    1.  
    2. public class GameManager : MonoBehaviour
    3. {
    4. public GameObject ballPrefab;
    5. GameObject _currentBall;
    6.  
    7. void BeginState(State newState)
    8.     {
    9.         switch (newState)
    10.         {
    11.             case State.MENU:
    12.                 panelMenu.SetActive(true);
    13.                 break;
    14.             case State.INIT:
    15.                 panelPlay.SetActive(true);
    16.                 Score = 0;
    17.                 Level = 0;
    18.                 balls = 100;
    19.                [B] Instantiate(ballPrefab);[/B]
    20.                 SwitchState(State.LOADLEVEL);
    21.                 break;
    22.  
    23.  
    24. void Update()
    25.     {
    26.        switch (_state)
    27.         {
    28.             case State.MENU:
    29.                 break;
    30.             case State.INIT:
    31.                 break;
    32.             case State.PLAY:
    33.             [B]    if (_currentBall == null)
    34.                 {
    35.                     if(balls > 0)
    36.                     {
    37.                         _currentBall = Instantiate(ballPrefab);[/B]
    38.                     }
    39.                     else
    40.                     {
    41.                         SwitchState(State.GAMEOVER);
    42.                     }
    43.                 }
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,160
    If the camera lives in the scene with the game manager, reference the camera (or specifically, your camera controller component) in the manager via the inspector.

    Then you can get the return on instantiating the ball, and use that to hook up the ball with the camera.
     
    patrick_Drake likes this.
  3. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    Instead of initiating the prefab100 times, you might be better off with turning SetActive on and off when needed.

    Your followTarget would be your prefab, dragged in place in inspector. When the player makes a mistake, you use SetActive(false). In your Update() you check for activeSelf value and balls > 0, use SetActive(true) when it is not game over (yet).
     
  4. riasillo

    riasillo

    Joined:
    May 23, 2023
    Posts:
    25
    update. got answer from a friend. appreciate suggestions. thanks.

    Code (CSharp):
    1. Transform followTarget;
    2.  
    3. private void Update()
    4.  
    5.     {
    6.         if(followTarget == null)
    7.         {
    8.             followTarget = GameObject.FindGameObjectsWithTag("Ball")[0].transform;
     
  5. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    Happy for you it is working, just know there are other ways to do this.
     
    riasillo likes this.
  6. riasillo

    riasillo

    Joined:
    May 23, 2023
    Posts:
    25
    last method actually was kicking off an array error. changed to this and solved.
    hope it can help someone else.

    Code (CSharp):
    1.  private void Update()
    2.  
    3.     {
    4.      
    5.         if(followTarget == null)
    6.         {
    7.             var balls = GameObject.FindGameObjectsWithTag("Ball");
    8.             if(balls.Length > 0)
    9.             {
    10.                 followTarget = balls[0].transform;
    11.             }