Order of initializing (networked) objects
Hello, This has probably been explained multiple times before, but I couldn't figure out how to google it right.
How do you instantiate objects the right way, which have an initialization method that depends on (public) attributes that are set from outside? When Instantiate() is called, Awake and Start are invoked. So you don't have any opportunity to set any attributes that are crucial for the initialization. Thus you have to call an actual initialization again, after Start.
The only solution that I found so far are Coroutines in the Start method. (Making a LateStart() that is called an arbitrary amount of time after start seems weird...), or checking in the Update method each frame (which seems unnecessary idk) if the crucial attribute has been set from outside (see code example)
using UnityEngine.AI;
using UnityEngine;
public class Delme : MonoBehaviour {
public Transform destination;
void Start () {
GetComponent<NavMeshAgent>().SetDestination(destination.transform.position);
}
void Update () {
if (destination == null)
return;
}
}
Your answer
Follow this Question
Related Questions
Instantiate not repeating? 1 Answer
Destroy a prefab from another class 2 Answers
[Asset Bundle] Split the prefabs into a single gameobject 0 Answers
instantiate capacity 0 Answers
Instantiation help 0 Answers