camera follow instantiated Prefab?

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.
[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...

```csharp
**public class GameManager : MonoBehaviour
{
public GameObject ballPrefab;
GameObject _currentBall;

void BeginState(State newState)
{
switch (newState)
{
case State.MENU:
panelMenu.SetActive(true);
break;
case State.INIT:
panelPlay.SetActive(true);
Score = 0;
Level = 0;
balls = 100;
[B] Instantiate(ballPrefab);[/B]
SwitchState(State.LOADLEVEL);
break;

void Update()
{
switch (_state)
{
case State.MENU:
break;
case State.INIT:
break;
case State.PLAY:
[B] if (_currentBall == null)
{
if(balls > 0)
{
_currentBall = Instantiate(ballPrefab);[/B]
}
else
{
SwitchState(State.GAMEOVER);
}
}**
```

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.

1 Like

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).

update. got answer from a friend. appreciate suggestions. thanks.

Transform followTarget;

private void Update()

    {
        if(followTarget == null)
        {
            followTarget = GameObject.FindGameObjectsWithTag("Ball")[0].transform;


Happy for you it is working, just know there are other ways to do this.

1 Like

last method actually was kicking off an array error. changed to this and solved.
hope it can help someone else.

 private void Update()

    {

        if(followTarget == null)
        {
            var balls = GameObject.FindGameObjectsWithTag("Ball");
            if(balls.Length > 0)
            {
                followTarget = balls[0].transform;
            }