- Home /
A bit complicated instantiate question
So i have 3 meteors, they are all disabled. I want to make it so that with a script i can call a function, that will spawn a copy of the disabled meteor, undisable it, let it fall across the screen, then destroy the gameobject. So that at any time, i can call the function and have a meteor fall down. I also want to randomize which one falls down so that constantly a random meteor falls down every few seconds. So it creates the effect that you are flying through a whole lot of meteors, but its infinite.
You don't need to keep enabling and disabling meteors. Just make a meteor prefab and define a list of possible spawn locations. Then choose one once in a few seconds randomly using Random.Range() function.
I'm not that good enough to write a script off of that information, do you $$anonymous$$d writing like an example script (javascript please), showing me how i can do it, i can do it myself from there
Ok I'll help you out, but it's really important that you learn it.
Answer by thornekey · Mar 09, 2014 at 12:00 AM
like Nick4 said, you can define a list of spawn places, if you create an array and hold the spawn places in there you can randomly choose different meteor prefabs (assuming u want some uniqueness) and then randomly spawn the amount, and random range location of them.
For example (this wont be 100% accurate nor complete but an outline)
private bool CheckForMeteorPrefabs() {
if (meteorPrefab.Length > 0)
return true;
else
return false;
}
private bool CheckForSpawnPoints() {
if (spawnPoints.Length > 0)
return true;
else
return false;
}
//generate a list of available spawnpoints that do not have any meteors childed to it
private GameObject[] AvailableSpawnPoints() {
List<GameObject> gos = new List<GameObject>();
for(int cnt = 0; cnt < spawnPoints.Length; cnt++) {
if (spawnPoints[cnt].transform.childCount == 0) {
Debug.Log("**meteor spawn point available**");
gos.Add(spawnPoints[cnt]);
}
}
Answer by superluigi · Mar 08, 2014 at 11:59 PM
Seems like you already have it figured out. You're going to want to create 3 variables and store your meteors there. You will then want to "Instantiate" using "Random.Range" to randomize between your 3 meteor variables. You can then start an "InvokeRepeating" loop to "Instantiate" every whatever timespan you choose. The words in "" are the keywords you want to check out. I am willing to help you out with this and help you write and explain anything you need for as long as you need me to. However, I am not willing to write the code for you as that won't really help you at all.
#pragma strict
var m1 : GameObject;
var m2 : GameObject;
var m3 : GameObject;
InvokeRepeating("SpawnMeteors", 2, 2);
function SpawnMeteors()
{
var meteors : Array = Array(m1,m2,m3);
var position : Vector3 = Vector3(Random.Range(-10.0, 10.0), 0, 0);
Instantiate(meteors[Random.Range(0, meteors.length)], position, m1.transform.rotation);
}
This is tested an confirmed to work. If however you run into any problems let me know.
Also if you need me to explain any of the lines, or all of them, let me know. I'll tell you what each of them does.
Ok i have found a few problems but there is no errors. Ok so one problem is that when it first starts the 3 meteors fall down on the screen making it impossible to avoid. If i disable the meteors then it just spawns more disabled meteors. Also it never deletes the copies. Plus they don't spawn in the right spot. But i think i can fix that last problem on my own its just the first meteors spawn and fall and i can't really change that because if i disable them then the script makes more disabled meteors
And dude, thank you so much. I could have never written this script by myself and i want to be able to do so someday. I'm 13 and i have a lot of time to learn :)
How do i make it so that after some time they get deleted?
Or mabye i could make a box collider with a function to break the gameobject that enters it
Your answer
Follow this Question
Related Questions
Spaceing out objects 2 Answers
Best way to respawn car? 0 Answers
How to activate a button? 1 Answer
Instantiate problem in a RTS game 0 Answers