- Home /
Static target in the srcipt for my Prefab
I have an FPS Character Controller and an Enemy.When The FPScc enters in to the trigger it makes the Enemy prefab invite to the scene and this enemy has a follow script and a target "Transfrom" variable. The problem is if I delete the Enemy from the scene( because I want it to appear if I hit the trigger...) the Transform variable lose the my target and cant invite the prefab. Can I define my target manually in the script?
Answer by t0ky1337 · Jan 05, 2013 at 03:22 PM
Okey you have exaggerated this a little bit :D. Btw I figured out the answer what I want:
public var thePlayer : Transform;
thePlayer = GameObject.Find( "First Person Controller" ).transform;
Answer by Davidovich · Jan 05, 2013 at 12:38 AM
Yes.
In the OnTriggerEnter function where you Instantiate the enemy prefab you just need to hand it the new target to follow.
EG. (in C#)
public Object enemyPrefab;
void OnTriggerEnter()
{
GameObject enemy = (GameObject) Instantiate(enemyPrefab, Vector3.zero, Vector3.zero);
enemy.GetComponent("Follow").target = gameObject.transform;
}
Warning Untested code that assumes things about your scripts. May not work at all.
Couple of issues here, the third parameter of Instantiate takes a quaternion not a vector3. Also, you better go with:
enemy.GetComponent<Follow>().target = transform;
Second case is more about performance.