- Home /
Set a Prefab's variable when you instantiate it, but before it calls Awake?
I'm using UnitySteer, and its SteerForTarget component destroys itself if its target value is not set on Awake. I can't figure out how to spawn a vehicle that has that component without it destroying itself.
Answer by Jimmie · Apr 29, 2011 at 06:08 PM
I don't know the answer to this, but I was just dealing with a similar problem. I wanted a prefab to reference something in the Scene as its Target. What I ended up doing was creating a near public String TargetTag, and modified SteerForTarget behavior to this:
public string TargetTag; void Awake() { if (TargetTag != null) { GameObject gob = GameObject.FindWithTag(TargetTag); Target = gob.transform; }
if (Target == null)
{
this.enabled = false;
}
}
Answer by AudiOishi · May 16, 2011 at 08:29 PM
I have not fully tested this yet, but one option is to set the SteerForTarget script's "target" property to it's own transform.
Since you can't set the target property of a prefab, first instantiate the prefab manually by dragging it into the game view. Then set the newly instantiated SteerForTarget.Target property to the GameObjects own transform. Now update the prefab by dragging the GameObject from the Hierarchy back into the Project view.
Now you should be able to set a new Target in your code.
I know it's a bit of a hack, but it works okay for me so far.
Your answer
Follow this Question
Related Questions
Removing A Component From An Instantiated Prefab After X More Are Instantiated 1 Answer
Get child component of a prefab to reference with another prefab 1 Answer
Prefab Cannot Select Object Correctly 0 Answers
Prefab instantiates with its components disabled 3 Answers
Unity Steer - Setting Quarry in Steer for Pursuit problem 1 Answer