i hope you will understand my question. I am trying to create level prograss bar, here is the script to get distance between ball and finish line , here finish line is prefab clone which is randomly instantiated at different position, but when i am using finsih line prefab to finishTransform its using finsih line prefab position (0, 0, 0) not from prefab clone
[SerializeField] private Transform startTransform;
[SerializeField] private Transform finishTransform;
[SerializeField] private PlayerController ball;
public float EntireDistance { get; private set; }
public float DistanceLeft { get; private set; }
private void Start()
{
EntireDistance = finishTransform.position.z - startTransform.position.z;
}
but when i am using finsih line prefab to finishTransform its using finsih line prefab position (0, 0, 0) not from prefab clone
This is the expected behaviour since you have provided a prefab and not the clone.
What you need to do is to retrieve the clone into your script when it is instantiated.
There are several ways to do so. Here is an example:
[SerializeField] private Transform startTransform;
[SerializeField] private PlayerController ball;
private Transform finishTransform; // You don't need to provide the finish line in edit mode
public float EntireDistance { get; private set; }
public float DistanceLeft { get; private set; }
public void SetFinishLine(Transform finishLine)
{
finishTransform = finishLine;
}
private void Start()
{
EntireDistance = finishTransform.position.z - startTransform.position.z;
}
// Spawner script
[SerializeField] SCRIPT_NAME script; // SCRIPT_NAME = name of the previous script
public void SpawnFinishLine()
{
GameObject finishLine = Instantiate(finishLinePrefab, ....)
script.SetFinishLine(finishLine.transform);
}