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 Bill Cosby · May 08, 2013 at 06:16 AM · javascriptinstantiateprefabjava

Problems instantiating a prefab (javascript)

Hello, I'm new to using javascript and I am having some trouble.

In my assets folder, I have a prefab named "enemyprefab", and I am trying to make a script so a copy of the prefab will spawn at the location of the object I have attatched the script to. Looking at the Unity manuals on the instantiate command and its discussion of instantiating prefabs, led me to believe that this code:

 #pragma strict
 var enemyprefab : Transform;
 function Start () {
 
 
 Instantiate(enemyprefab, Vector3 (x, y, 0), Quaternion.identity);
 
 }
 
 function Update () {
 
 }

would give me the result I need, but unfortunately it doesn't work at all. I've looked at other questions on this website about this topic, but I just can't figure out what to do.

Any help is appreciated.

Comment
Add comment · Show 7
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 ahaykal · May 08, 2013 at 06:34 AM 0
Share

what errors are you getting?

What about the x,y variables? Did you create these variables? Did you assign them?

avatar image CDK · May 08, 2013 at 07:15 AM 0
Share

You have dragged the enemyprefab prefap into the inspector and onto the enemyprefab thing inside this script?

avatar image Bill Cosby · May 08, 2013 at 03:39 PM 0
Share

I've tweaked the script a bit so that the x, y, and z, variables are now created and assigned, and I've now put the enemyprefab prefab onto the script with the inspector and there are no longer any errors, but nothing seems to be spawning. The code now looks like:

 #pragma strict
 var enemyprefab : Transform;
 var x: float;
 var y: float;
 var z: float;
 
 function Start () {
 
 
 Instantiate(enemyprefab, Vector3 (x, y, z), Quaternion.identity);
 
 }
 
 function Update () {
 
 }
avatar image smnerat · May 08, 2013 at 04:24 PM 0
Share

In my experience I never got this to work either. Try this.

 #pragma strict
 var enemyprefab : Transform;
 var clonedObject : Transform;
 var x: float;
 var y: float;
 var z: float;
  
 function Start () {
  
  
 clonedObject = Instantiate(enemyprefab, Vector3 (x, y, z), Quaternion.identity);
  
 }
  
 function Update () {
  
 }
avatar image ahaykal · May 08, 2013 at 04:41 PM 0
Share

hey check if it is instantiating in the hierarchy, if it is, it maybe the position is far away from the camera.

Show more comments

1 Reply

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

Answer by Vonni · May 08, 2013 at 06:02 PM

  1. Why are you using 3 floats and not just Vector3().

  2. In your question you said you wanted it to spawn where the script object is located, use this.

      #pragma strict
         var enemyprefab : Transform;
         var clonedObject : Transform;
         var relativePositionOffset : Vector3 = Vector3(0,0,0);
          
         function Start () {
          
          
         clonedObject = Instantiate(enemyprefab, transform.position + relativePositionOffset, Quaternion.identity);
          
         }
          
         function Update () {
          
         }
    
    

If this doesn't work then I don't know what you are doing wrong. This DOES work.

Comment
Add comment · Show 1 · 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 Bill Cosby · May 08, 2013 at 07:14 PM 0
Share

That works perfectly. That's so much for you help. This is really great.

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Instantiate at wrong position 1 Answer

Unity Frame Drop From Instantiating Prefab that has script 0 Answers


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