- Home /
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";
}
(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?
No thats not the case, Im taking a GameObject from a prefab.
Yes thats right, The rena$$anonymous$$g
It would be far simpler just to use arrays, so you don't need a bunch of separate objects and functions like that.
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;
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";
}
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.
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
Follow this Question
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