- Home /
How to access a clone that is instantiated prefab object
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;
}
Answer by Hellium · Apr 10, 2020 at 11:53 AM
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);
}
Thank you very much , i am getting this error can you please check whats that means
$$anonymous$$y bad, replace
script.SetFinishLine(finishLine);
by
script.SetFinishLine(finishLine.transform);
thanks it works fine , forgot to mention before i am having another error
here Game$$anonymous$$anager means script name
![alt text][2]
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
Flip over an object (smooth transition) 3 Answers