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 SLIMEBASS · May 27, 2013 at 03:50 PM · javascriptinstantiateclone

Instantiate without (clone) in JS.

Title says it all. I have came this far, Got no errors but it aint woking.

     var spawnPoint : Transform;  

     var object1 : GameObject;  

     var object2 : GameObject;  

     var object3 : GameObject;  

     var object4 : GameObject;  

     var amountEnemies = 200000;  

     var yieldTimeMin = 2;  

     var yieldTimeMax = 5;  

 
     function Start()
     {
         Spawn();
     }
 
     function Spawn() 
     { 
         for (i=0; i<amountEnemies; i++)
         {
             yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));
 
             var random = Random.Range(0, 4);
             if (random == 1)
             {
                 Spawn1();
             }
             if (random == 2)
             {
                 Spawn2();
             }
             if (random == 3)
             {
                 Spawn3();
             }
             if (random == 4)
             {
                 Spawn4();
             }
         }
     }
 
     function Spawn1()
     {
         Instantiate(object1, spawnPoint.position, spawnPoint.rotation);
         object1.name = "Deagle";
     }
 
     function Spawn2()
     {
         Instantiate(object2, spawnPoint.position, spawnPoint.rotation);
         object2.name = "MP5KA4";
     }
 
     function Spawn3()
     {
         Instantiate(object3, spawnPoint.position, spawnPoint.rotation);
         object3.name = "SWT-25";
     }
 
     function Spawn4()
     {
         Instantiate(object4, spawnPoint.position, spawnPoint.rotation);
         object4.name = "Blaser R93";
     } 
Comment
Add comment · Show 3
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 ExTheSea · May 27, 2013 at 04:04 PM 0
Share

(Clone) normally gets attached to a gameobject if there is a similar object already in the scene. Is that the case?

Also what is not working? The rena$$anonymous$$g?

avatar image SLIMEBASS · May 27, 2013 at 04:11 PM 0
Share
  1. No thats not the case, Im taking a GameObject from a prefab.

  2. Yes thats right, The rena$$anonymous$$g

avatar image Eric5h5 · May 27, 2013 at 06:04 PM 0
Share

It would be far simpler just to use arrays, so you don't need a bunch of separate objects and functions like that.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Owen-Reynolds · May 27, 2013 at 04:26 PM

Instantiate returns a link to what you just created, which you can then use to play with it. The code above doesn't catch that link. It changes the original prefab, not the Instantiated copy.

The man page: http://docs.unity3d.com/Documentation/Manual/InstantiatingPrefabs.html and the relevant lines (most people would use Transform or GameObject. Rigidbody is because they know they only want to change the speed.)

 var rocketClone : Rigidbody = Instantiate(rocket, transform.position, transform.rotation);
 rocketClone.velocity = transform.forward * speed;
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 SLIMEBASS · May 27, 2013 at 04:47 PM 0
Share

Im not 100% sure what you mean but what you are saying is that the script you provided will spawn a object called rocketClone?

If thats so here is my code but im getting the same error on every line where im instantiate: The error:

 SpawnPt2.js(49,51): BCE0022: Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.Transform'.

The code:

 var spawnPoint : Transform;
 var object1 : GameObject;
 var object2 : GameObject;
 var object3 : GameObject;
 var object4 : GameObject;
 var amountEnemies = 200000;
 var yieldTime$$anonymous$$in = 2;
 var yieldTime$$anonymous$$ax = 5;
 
 function Start()
 {
     Spawn();
 }
 
 function Spawn() 
 { 
     for (i=0; i<amountEnemies; i++)
     {
         yield WaitForSeconds(Random.Range(yieldTime$$anonymous$$in, yieldTime$$anonymous$$ax));
 
         var random = Random.Range(0, 4);
         if (random == 1)
         {
             Spawn1();
         }
         if (random == 2)
         {
             Spawn2();
         }
         if (random == 3)
         {
             Spawn3();
         }
         if (random == 4)
         {
             Spawn4();
         }
     }
 }
 
 function Spawn1()
 {
     var spawnObject1 : Transform = Instantiate(object1, spawnPoint.position, spawnPoint.rotation);
     //object1.name = "Deagle";
 }
 
 function Spawn2()
 {
     var spawnObject2 : Transform = Instantiate(object2, spawnPoint.position, spawnPoint.rotation);
     //object2.name = "$$anonymous$$P5$$anonymous$$A4";
 }
 
 function Spawn3()
 {
     var spawnObject3 : Transform = Instantiate(object3, spawnPoint.position, spawnPoint.rotation);
     //object3.name = "SWT-25";
 }
 
 function Spawn4()
 {
     var spawnObject4 : Transform = Instantiate(object4, spawnPoint.position, spawnPoint.rotation);
     //object4.name = "Blaser R93";
 } 
avatar image Owen-Reynolds · May 27, 2013 at 10:11 PM 0
Share

It's not my script -- it's a Unity doc. The link explains what it's doing.

For the name, you'd say spawnObjectX.name = "cute kitty"; (once it was working.)

The transform/gameObject thing, that's surprisingly hard to find for javascript. All the examples assume that if you're doing anything tricky, you must be using C#. Instantiate wants to make a GameObject (sort of,) not a Transform. Searching "unity Instantiate javascript" gave a lot of results, but most were just a naked Instantiate, so no help there.

avatar image Eric5h5 · May 27, 2013 at 11:29 PM 0
Share

That's actually not the case; in Unityscript, Instantiate returns whatever the type of object it is that you're instantiating. So if you have a variable called "someTransform" of type Transform, then this:

 var clone = Instantiate (someTransform);

returns Transform, not GameObject. So the code posted by SLI$$anonymous$$EBASS will not work since you can't assign a GameObject to a Transform.

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

Make a conformed copy of a clone as a 2Dtexture 0 Answers

Code only affects first clone? 2 Answers

Instantiating a random dropped consumable item from many cloned objects 1 Answer

How to keep instatiated object above a certain height? 1 Answer

Following Object can't find newly Instantiated Object (Solved) 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