- Home /
Multi Random Spawn Enemies / Item
I can not think of what I should do. I need help in the picture you can see that I have spawn points and that I can decide how many I want, of a completely simple empty object that is my spawn point. and you will see that I have frefabs for items or enemies. but what I want to change that for each element in spwan point I want applicants feature prefabs.
I want it to look like this """this photo is made in paint""" XD
you might already understand what I want.
that I may be able to place different things in the different spawn. so that not all spawns, spawns from the same frefabs list
here is the Spawn E&I script.
var spawnPoints : Transform[]; // Array of spawn points to be used.
var Prefabs : GameObject[]; // Array of different Enemies or Item that are used.
function Start(){
Spawn();
}
function Spawn(){
var obj : GameObject = Prefabs[Random.Range(0, Prefabs.length)]; // Randomize the different enemies or Item to instantiate.
var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)]; // Randomize the spawnPoints to instantiate enemy or Item at next.
Instantiate(obj, pos.position, pos.rotation);
}
:D BIG ThX to you who will help me, Really appreciate it :)
Answer by OtsegoDoom · Apr 22, 2014 at 05:53 PM
You'll want to set up a class for your spawn points.
var spawnPoints : SpawnPoints;
class SpawnPoints {
var spawnTransform : Transform;
var prefabs : GameObject[];
}
You can then access each element of the class like so:
var obj : GameObject = spawnPoints[x].prefabs[Random.Range(0, spawnPoints[x].prefabs.length)];
Ex. spawnPoints.spawnTransform = gameObject.transform;
how would it look like in the script
Big thx to you
Try it and find out :D
It should look the same actually, with one exception. Ins$$anonymous$$d of the line: Element 0 Spawn1 (Transform)
It would look like: Element 0 Spawn Transform Spawn1 (Transform)
i got this error :P
Assets/Spawn E&I.js(4,5): BCE0089: Type 'Spawn E&I' already has a definition for 'spawnPoints'.
That would probably mean you've already declared spawnPoints somewhere else in the script. $$anonymous$$ake sure you only declare it once. Cheers
Something like this maybe:
var spawnPoints : SpawnPoint[];
class SpawnPoint{
var spawnTransform : Transform;
var prefabs : GameObject[];
}
function Start(){
Spawn();
}
function Spawn(){
var spawn : SpawnPoint = spawnPoints[Random.Range(0, spawnPoints.length)];
var obj : GameObject = spawn.prefabs[Random.Range(0, spawn.prefabs.length)];
Instantiate(obj, spawn.spawnTransform.position, spawn.spawnTransform.rotation);
}
Answer by delorean225 · Apr 23, 2014 at 04:47 PM
The question's already answered, but just a reminder: RandomRange is inclusive/exclusive. So if you make a float value that's a Random.Range(0, 5) It can be any number from 0 to 4. An Int would be 0 to 4.9999999999999999999999999... you get the point. So to make the whole list, you need to say Random.Range(0, ValueYouWant.length + 1) to include the top.
it surely can not be, i don't understand you there ? :3
To put it more simply, Random.Range isn't picking a number from x to y. It's picking a number that's greater than or equal to x and any number that's lower than y. Pretty much, if you leave it as x to y, it is going to pick numbers from x to y-1. You have to choose x and y+1 to get the full size of y.
Wrong way round:
RandomRange(0, 5) -> either 0, 1, 2, 3, 4.
RandomRange(0, parseFloat(5)) -> between 0 and 5 (including 4.9999)
if you have a list of length 3 then you want to use RandomRange(0, 3), if you used RandomRange(0, 4) //length + 1 it would occasionally return 3 and index 3 doesn't exist in a list of length 3 as they are zero-indexed.
your script is fine, delorean is incorrect, you would get some errors occasionally if you did Random.Range(0, ValueYouWant.length + 1) as he suggested.
Hmm. The scripting reference says you're right... I probably was looking at a generic JS thing. Sorry.
Answer by Candyman89 · May 07, 2014 at 08:42 AM
Sorry, I have a question... when you set the Random.Range, shouldn't be the screen's width and height the parameters? So the items and enemies appears inside the screen... I'm pretty new at this... greetings :)
no, in this script you need to place out spawns where they are co$$anonymous$$g from, they dont random spawn in the scene/map that only random choose items/enemies to be dropped from the spawn.
hope this was the answer you're looking for, if I understand your question right
Your answer
Follow this Question
Related Questions
creating an object using javascript 1 Answer
change a material from multiple materials 1 Answer
change skybox via script help ? 1 Answer
How to Implement System.StringBuilder into JavaScript? 2 Answers
Setting Scroll View Width GUILayout 1 Answer