This must be one of those super obvious things but I haven’t yet found the solution in all my searching.
Here’s my setup: I have a prefab with a script that needs a link to a transform using “var wayPoint : Transform”. But for some reason you can’t link Transform to prefabs unless they’re already in the Scene. How can I instantiate the prefab from the Project with all the links pre setup? Note I need the transform cus I’m using vector3.lerp to move the AI to a point - maybe there’s a better way to set it up?
And on a somewhat related question, is it better to instantiate a prefab from the Hierarchy (scene) or from the Project (prefab folder)?
Well, unless the object you want linked is part of the prefab, it isn’t possible to link it using a project prefab.
The best you can do is tag the waypoint using a unique tag and use GameObject.FindWithTag(). Alternatively, you could link the waypoint to the script inside the scene that creates the object in question and then send that waypoint to them on creation.
Thanks for the reply – I’ve only had internet via iPhone for the past few weeks so I haven’t been able to do a proper response until now.
Following your advice, here’s how I ended up cloning my enemies at the spawn points.
function SpawnTypeA() {
var spawnPointsFWD = GameObject.FindGameObjectsWithTag("spawnfwd");
var thisSpawnPoint : GameObject = spawnPointsFWD[Random.Range(0,spawnPointsFWD.Length)];
spawnPos = thisSpawnPoint.transform;
Instantiate(Resources.Load("AI_TypeA"),spawnPos.position, spawnPos.rotation);
}
There are about 5 GO’s tagged “spawnfwd” and this script chooses one at random each time the function is called.
One thing I haven’t figured out is if there’s a better way to find and store the spawn points than to do it every time the function is called. Like can I do it at Start() instead? When I move the first line to start like this:
function Start() {
var spawnPointsFWD = GameObject.FindGameObjectsWithTag("spawnfwd");
}
I get this error: Unknown identifier: ‘spawnPointsFWD’.
Well, it shouldn’t be giving you that error unless you’re trying to access the variable from a different location. Try declaring the variable outside of all the functions and then assigning it in Start().
var spawnPointsFWD : Transform;
function Start()
{
spawnPointsFWD = GameObject.FindGameObjectsWithTag("spawnfwd");
}