- Home /
Randomly Generate Game Object
hello.. I am developing 2D game. In my Game Scene.. , I am having two kind of enemy, I want to randomly instantiate enemy from these two types..
At a time i want only one instance of enemy from these two. So when one instance of the enemy destroyed after that another instance will be generated..
But for all this to work.. i want some idea .. I am not finding the way for randomly generation of object ..
Pleaze anyone suggest some idea and solve my problem..
Thanx in advance for your help and support..
Update :
Currently i have created single enemy object in GameController Script:
function Update ()
{
if(gameStarted)
{
if(level == 1)
{
if(!once)
{
totalEnimies = 20;
objectiveScore = 660;
enemyObject1 = Instantiate(enemyPrefab1 , new Vector3(0 , 6.892054 , -6.72) ,Quaternion.Euler(0,-90,90));
once = true;
}
}
}
}
Script Attached to my Enemy Prefab :
function Start () {
gameControllerScript = GameObject.Find("Main Camera").GetComponent("GameController");
scoreControllerScript = GameObject.Find("Main Camera").GetComponent("ScoreController");
SetPositionandSpeed();
}
function Update () {
var rotationspeed : float = currentRotationSpeed * Time.deltaTime;
var amttomove : float = currentSpeed * Time.deltaTime;
transform.Translate(Vector3.down * amttomove , Space.World);
transform.Rotate(new Vector3(0 , -1 , 0) * rotationspeed );
if(transform.position.y <= -6.00f)
{
SetPositionandSpeed();
}
}
function SetPositionandSpeed()
{
currentRotationSpeed = Random.Range(minRotationSpeed , maxRotationSpeed);
currentSpeed = Random.Range(minSpeed , maxSpeed);
x = Random.Range(-4.00f , 4.00f);
z = -6.72f;
y = 8.0f;
transform.position = new Vector3(x , y , z);
if(gameControllerScript.gameStarted)
scoreControllerScript.ManageEnimies();
}
Show us what you have done so far so that we could go from there.
$$anonymous$$y train of thoughts leads me to there:
Create another function that instantiates a random enemy, If you are using collision or triggers then when there is a collision/trigger then call the instatiate enemy function and destroy the other object..
But here i am not destroying enemy.. i just change the position when it goes out of range,.. and if i destroy enemy on boundry cross then at that time do i need to call that function.. ??
Answer by MrVerdoux · May 07, 2013 at 10:27 AM
To make this kind of things a make a manager of enemies in which I create a public array of game objects with every monster you want.
Whenever I want a monster instantiated I call the Random class in unity to give me an int that goes from zero to the length (-1) of the monsters array.
Something like this:
public GameObject[] monsters;
int randomInt = Random.Range(0,monsters.Length-1);
Instantiate(monsters[randomInt]);
Note that the code above might not be exactly what you want but it's one of the ways of achieving it.
Edit:
if you want to do this when the monster is destroyed you could also use the function
void OnDestroy(){
//Generate a new random monster here
}
The code you write in the OnDestroy function will be called when the monster is destroyed. I just added it because I thought it might be useful to you.
By the way, if you are happy with the answer you should mark your question to "answered", so other people would see your question and benefit from the answer as well. (however if you don't like the answer you don't have to do anything).
Your answer
Follow this Question
Related Questions
Can't add component because class doesnt exist 1 Answer
Best Way - Animation - Multiple GameObjects. 0 Answers
Collision On Specific Object= Destroy not working. 2 Answers
How to form copies of yourself that follow you when you walk? (2D Platformer) 2 Answers
Building game like prison architect. How to manage sprites? 0 Answers