- Home /
Using Prefab as Object when calculating distance
So I am basically trying to get the entire Distance thing to work(I am working in C#), but I keep getting the error "UnassignedReferenceException: The variable Destination of 'Player' has not been assigned", I am pretty sure it has to do with the fact that I'm trying to use a prefab, but I have no idea what else to use in this case(Still new to this), my code looks like this:
public GameObject DestinationPrefab;
void Update () {
float distance = Vector3.Distance(transform.position, DestinationPrefab.transform.position);
if (distance <= 100)
{
Destination destination = (Destination)gameObject.GetComponent("Destination");
destination.moveDestination();
}
}
Your code should check to make sure that DestinationPrefab is not null, and also that destination is not null. (Also, in C# you can use gameObject.GetComponent() so you don't need the "(Destination)" typecast.)
"Your code should check to make sure that DestinationPrefab is not null" Can you give me a code example?
Answer by DaveA · Feb 04, 2011 at 12:59 AM
Did you remember to drag/drop said prefab onto the DestinationPrefab in the Inspector?
Well... DestinationPrefab IS the prefab I'm talking about, And I'm fairly sure I have dragged it to the right place