- Home /
How to instantiate a Prefab ?
Hi everyone. my question is: how to instantiate a prefab during runtime, with a javascript ?:
-prefab name: Waypoint
-the script will not be attached to the prefab but it will be attached to the camera: I want the camera to instantiate the prefab 'Waypoint' to the coordinate of the camera and with no particular rotation.
-if the above is not possible, then I would like to attach the script to prefab 'Waypoint' itself and instantiate the 'Waypoint' prefab to the coordinates of the camera: how can I do ?
Answer by Novodantis 1 · Jun 22, 2010 at 03:49 PM
If you want the script to seed them repeatedly over time (not many uses in just creating one, before the game even reaches the first frame) then you can modify the script quite easily, as such:
var seedRate = 5.0; // creates 1 waypoint per 5 seconds
private var timer = 0.0;
function Update() {
if (timer > seedRate) {
Instantiate(Waypoint,transform.position,Quaternion.identity);
timer = 0;
} else {
timer += Time.deltaTime;
}
}
"Waypoint" here should point to a variable. add "var Waypoint : GameObject" to the top of the script then drag your prefab onto the script in the Inspector
Your solution is exactly like $$anonymous$$e, he didn't ask for a timer or something, just an instantiation.
Sorry, I meant to be clearer, that's what I meant by "modify the script". I would have added it as a comment but the code got too unwieldly. To me, waypointing pretty much implies more than one node, but I guess that might be done with different objects.
Answer by AnaRhisT · Jun 22, 2010 at 03:28 PM
//put this code in a java script and attach it to ur main camera
function Start() {
Instantiate(Waypoint,transform.position,Quaternion.identity);
}
"Assets/WeaponScripts/CreerWaypoints.js(2,16): BCE0005: $$anonymous$$ identifier: 'Waypoint'." my console display this message.
Assets/WeaponScripts/CreerWaypoints.js(2,16): BCE0005: $$anonymous$$ identifier: 'Waypoint'.
-where 'CreerWaypoint.js' is my script (it means create waypoint in french)
dont use capitals in variables. Unity tends to confude those with functions or classes!
Your answer
Follow this Question
Related Questions
Instantiate a prefab at runtime 2 Answers
Combining Meshes? 1 Answer
Put a prefab in Hierarchy without using Instantiate ? (from code) 2 Answers
Scripting a prefab to duplicate itself within a fixed radius 2 Answers
Bullet Instantiation 1 Answer