Ball.transform is not updating in Update() function

Hi all,

quick question, that got me frustrated a little. My first thought when making the “camera follows ball” challenge was just like Ben did, I thought myself i have the ball, calculate the fix offset in Start() and then calculate each frame the new position, since the update will automatically have the new ball position.

It didn’t work, and by logging out the ball transform it showed that the update did not recalculate the transform of the ball, it sticked to it’s initial starting position, even though the ball was moving

After some trying around, i followed the old pattern and added

ball = GameObject.FindObjectOfType<Ball>();

into the Start() method, and now it works for me too.

Why is that ? I have the “public Ball ball” Variable set at the script beginning just like Ben has.

Is this maybe a version issue ? I wanted to switch to Unity 2017 with the following 3 classes, but accidently made the BowlMaster in unity 5.5.4p4. What is Ben using for his lessons ? Was Unity 2017 even out when recording ?

I want to know that, so that I can better understand how to deal with objects in scripts.

Thanks for helping :slight_smile:

(edit) I know that at the start of the lesson there was a hint to use unity 5.6 or higher, I thought I have the newest version since “update” always told me my inspector is up to date… Just found the unity 5.6.5 version, trying that now…

Can you post your camera script? I really doubt this has to do with the version of Unity.

1 Like

This is my script :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraControl : MonoBehaviour {

    public Ball ball;

    private Vector3 offset;

	// Use this for initialization
	void Start () {

        //ball = GameObject.FindObjectOfType<Ball>();
        offset = transform.position - ball.transform.position;
		
	}
	
	// Update is called once per frame
	void Update () {

        transform.position = ball.transform.position + offset;
        print(ball.transform.position);

    }
}

Without the deactivated line, camera stays in plays, and ball.transform.position will always be logged as the balls start position, though the ball is moving

1 Like

Did you drag the actual ball game object from the scene into the public Ball field, or the prefab? I’d try re-making this link, since it seems to be what’s broken here.

1 Like

I did, and since I indeed prefabed camera and ball, I even deleted them and inserted the prefabs again, still the same reaction… Quite strange…

1 Like

Sorry I wasn’t clear, I meant you should NOT drag the prefab in this case, but the game object. If that’s what you did, I’m not sure what the problem could be.

Oh… now, THAT is working, that’s really confusing me a bit… I prefabed the stuff AFTER creating the initial script I think, not fully sure anymore…

I now dragged the ball object from the inspector on the camera, and with the instance it works…

But didn’t Ben said, always put “Prefab to Prefab” ??? :slight_smile:

1 Like

It’s generally a more robust way to make these connections indeed, but there are cases (like this one) where it matters which actual instance of the prefab is concerned. If you had more than one ball in the scene, the camera would get a bit confused if it had to follow all of them at once :wink:

There are probably more technical reasons for it, but that’s how I think about it when linking game objects to public variables (which I tend to avoid anyway, so I probably would have just made a private variable and used your solution)

2 Likes

Well, after all that’s what I call a “good error”, now I know that I must take much more focus on these things, and when I have problems, also a possible location for the error.

Thank you for your time and help pointing me to the problem, I really appreciated that :slight_smile:

Cheers
Michael

1 Like

I made the same mistake of draggin in the prefab rather then the spawned object.

Thanks for the solution :wink:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms