- Home /
Question by
atyc94 · Aug 23, 2019 at 04:11 PM ·
prefab changing at runtime
How to stop prefab from resetting its user defined reference variable
I have a monster prefab which has the following movement script attached to it.
target is the destination for the monster when I instantiate it.
But as soon as I make the object as a prefab by dragging it into the assets,the target variable get reset resulting in the spawned mob just standing still
Only number variables stay constant.
Are there any workaround to allow the prefab to have a reference of the gameobject?
[SerializeField] GameObject target;UnityEngine.AI.NavMeshAgent agent;// Start is called before the first frame updatevoid Start(){agent = this.GetComponent();agent.SetDestination(target.transform.position);}// Update is called once per framevoid Update(){bool chase = agent.SetDestination(target.transform.position);if (chase == true){agent.SetDestination(target.transform.position);}}
Comment
Answer by YetAnotherKen · Aug 25, 2019 at 10:29 PM
The easiest solution I can think of is to assign the destination after instantiation.
Your answer