Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 $$anonymous$$e, 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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges