x


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 ?

more ▼

asked Jun 22 '10 at 03:25 PM

etxtra gravatar image

etxtra
2 2 2 5

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

//put this code in a java script and attach it to ur main camera

function Start() {
   Instantiate(Waypoint,transform.position,Quaternion.identity);
}
more ▼

answered Jun 22 '10 at 03:28 PM

AnaRhisT gravatar image

AnaRhisT
865 30 33 43

"Assets/WeaponScripts/CreerWaypoints.js(2,16): BCE0005: Unknown identifier: 'Waypoint'." my console display this message.

Jun 22 '10 at 03:44 PM etxtra

Assets/WeaponScripts/CreerWaypoints.js(2,16): BCE0005: Unknown identifier: 'Waypoint'.

-where 'CreerWaypoint.js' is my script (it means create waypoint in french)

Jun 22 '10 at 03:47 PM etxtra

lol? u forgot to add this; "var Waypoint : Transform;"

Jun 22 '10 at 04:04 PM AnaRhisT

dont use capitals in variables. Unity tends to confude those with functions or classes!

Apr 04 '11 at 10:01 PM Kroltan
(comments are locked)
10|3000 characters needed characters left

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;
  }
}
more ▼

answered Jun 22 '10 at 03:49 PM

Novodantis 1 gravatar image

Novodantis 1
1.7k 14 22 40

"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

Jun 22 '10 at 03:51 PM Novodantis 1

Your solution is exactly like mine, he didn't ask for a timer or something, just an instantiation.

Jun 22 '10 at 04:16 PM AnaRhisT

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.

Jun 22 '10 at 04:54 PM Novodantis 1
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3465
x1683
x1260
x374

asked: Jun 22 '10 at 03:25 PM

Seen: 5295 times

Last Updated: Jun 22 '10 at 03:25 PM