• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by etxtra · Jun 22, 2010 at 03:25 PM · javascriptinstantiateprefabruntime

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 ?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
-1
Best Answer

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;
  }
}
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Novodantis 1 · Jun 22, 2010 at 03:51 PM 0
Share

"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

avatar image AnaRhisT · Jun 22, 2010 at 04:16 PM 0
Share

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

avatar image Novodantis 1 · Jun 22, 2010 at 04:54 PM 0
Share

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.

avatar image
-2
Best Answer

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);
}
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image etxtra · Jun 22, 2010 at 03:44 PM 0
Share

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

avatar image etxtra · Jun 22, 2010 at 03:47 PM 0
Share

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

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

avatar image AnaRhisT · Jun 22, 2010 at 04:04 PM 0
Share

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

avatar image Kroltan · Apr 04, 2011 at 10:01 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How can I instantiate more prefabs as the score increases? 2 Answers

Prefab Spawning 2 Answers

How to instantiate on collision? 0 Answers

After instantiating an instance of a prefab and changing the value of a variable in a script attached to the prefab, that value is lost 1 Answer

Trouble working with a Custom class, JavaScript. 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges