- Home /
Need Help With Mission Gen.
Hi there, I have spent a few days trying to work out the best way to do my mission gen. I do struggle with the langue. What I want to do is to Spawn a random prefab at a random loaction in my game world, with some ranodmised stats. I also need to have th ablity to call certain stats from the prefab as well (e.g. Colour, Size, Ect.) My guess is i would have to add these to the prefab some how (but thats for later). Any Help on my Mission gen be very helpful.
#pragma strict
var enemyPrefabs : GameObject[]; // Array of different Enemies that are used.
var spawnPoints : Transform[]; // Array of spawn points to be used.
function Start () {
MissionGen();
}
function Update () {
}
function MissionGen () {
//if(mission = false){
// var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)]; // Randomize the different enemies to instantiate.
// var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)]; // Randomize the spawnPoints to instantiate enemy at next.
// (sudo Code)
// Randomly Genrate Name
// Randomly Genrated Name Part 2
// Randomly Genrate BackStory Part 1
// Randomly Genrated Backstroy Part 2
// Randomly Genrated Backstory Part 3
// mission = true
//
//
//}
//else{
// yeild WaitForSeconds (30);
// Start();
//}
}
function Check () {
// BadGuyCheck = GameObject.FindGameObjectsWithTag("BadGuy");
// If (BadGuy Check = 0){
// mission = false;
// + money Random.Range(1000,10000);
// yeild WaitForSeconds (30);
// Start();
// }
}
Answer by El Maxo · Mar 13, 2014 at 10:17 PM
#pragma strict
var enemyPrefabs : GameObject[]; // Array of different Enemies that are used.
var spawnPoints : Transform[]; // Array of spawn points to be used.
var Name1Choices : String[];
var Name2Choices : String[];
var Backstory1Choices : String[];
var Backstory2Choices : String[];
var Backstory3Choices : String[];
var mission : boolean = false;
var money : float;
function Start ()
{
MissionGen();
}
function MissionGen ()
{
if( !mission )
{
var pos : Transform = spawnPoints[Random.Range(0, spawnPoints.length)]; // Randomize the spawnPoints to instantiate enemy at next.
var spawnedEnemy : EnemyStats = Instantiate( enemyPrefabs[ Random.Range( 0, enemyPrefabs.length ) ], pos.position, pos.rotation ) as EnemyStats;
spawnedEnemy.Name1 = Name1Choices[ Random.Range( 0, Name1Choices.length ) ]; //Randomly Generated Name
spawnedEnemy.Name2 = Name2Choices[ Random.Range( 0, Name2Choices.length ) ]; //Randomly Generated Name Part 2
spawnedEnemy.Backstory1 = Backstory1Choices[ Random.Range( 0, Backstory1Choices.length ) ]; //Randomly Generated BackStory Part 1
spawnedEnemy.Backstory2 = Backstory2Choices[ Random.Range( 0, Backstory2Choices.length ) ]; //Randomly Generated Backstroy Part 2
spawnedEnemy.Backstory3 = Backstory3Choices[ Random.Range( 0, Backstory3Choices.length ) ]; //Randomly Generated Backstory Part 3
mission = true;
}
else
{
Invoke( "MissionGen", 30.0f );
}
}
function Check ()
{
var BadGuyCheck : GameObject = GameObject.FindGameObjectWithTag( "BadGuy" );
if ( BadGuyCheck == null )
{
mission = false;
money += Random.Range(1000,10000);
yield WaitForSeconds (30);
Invoke( "MissionGen", 30.0f );
}
}
Then for the other bit of code.
#pramga Strict
var Size : String;
var coulour : String;
I will have another bit ogf code at tells my GUI what to put out.
Answer by Jeremy2300 · Mar 13, 2014 at 06:05 PM
So, I'll try to help with this, and include some suggestions from your question here.
#pragma strict
var enemyPrefabs : GameObject[]; // Array of different Enemies that are used.
var spawnPoints : Transform[]; // Array of spawn points to be used.
var Name1Choices : String[];
var Name2Choices : String[];
var Backstory1Choices : String[];
var Backstory2Choices : String[];
var Backstory3Choices : String[];
var mission : boolean = false;
var money : float;
function Start ()
{
MissionGen();
}
function MissionGen ()
{
if( !mission )
{
var pos : Transform = spawnPoints[Random.Range(0, spawnPoints.length)]; // Randomize the spawnPoints to instantiate enemy at next.
var spawnedEnemy : EnemyStats = Instantiate( enemyPrefabs[ Random.Range( 0, enemyPrefabs.length ) ], pos.position, pos.rotation ) as EnemyStats;
spawnedEnemy.Name1 = Name1Choices[ Random.Range( 0, Name1Choices.length ) ]; //Randomly Generated Name
spawnedEnemy.Name2 = Name2Choices[ Random.Range( 0, Name2Choices.length ) ]; //Randomly Generated Name Part 2
spawnedEnemy.Backstory1 = Backstory1Choices[ Random.Range( 0, Backstory1Choices.length ) ]; //Randomly Generated BackStory Part 1
spawnedEnemy.Backstory2 = Backstory2Choices[ Random.Range( 0, Backstory2Choices.length ) ]; //Randomly Generated Backstroy Part 2
spawnedEnemy.Backstory3 = Backstory3Choices[ Random.Range( 0, Backstory3Choices.length ) ]; //Randomly Generated Backstory Part 3
mission = true;
}
else
{
Invoke( "MissionGen", 30.0f );
}
}
function Check ()
{
var BadGuyCheck : GameObject = GameObject.FindGameObjectWithTag( "BadGuy" );
if ( BadGuyCheck == null )
{
mission = false;
money += Random.Range(1000,10000);
yield WaitForSeconds (30);
Invoke( "MissionGen", 30.0f );
}
}
This is how you could do the EnemyStats class on a different script. You'd attach this script to the enemyPrefabs GameObjects and then their stories will be added to when spawned.
#pramga Strict
var Name1 : String;
var Name2 : String;
var Backstory1 : String;
var Backstory2 : String;
var Backstory3 : String;
Thanks that does look what i need. the only thing i want to keep stored are the shape and the colour not names :/. I could add the strings to this one. and just add a var of coulor to the others. although would i need a new piece of code for everyone?
Oh sorry, I was going off of the Pseudo Code in the above Code for the EnemyStats thing.
You can add any variables you'd like to this however. Even functions can be placed in the EnemyStats that can be called elsewhere or within.
A Start or Awake function in the EnemyStats scripts would be called once that object is instantiated (if that's helpful at all).
But you could easily add a var EnemyColour parameter or var EnemySize just like the Strings I had done previously.
Your answer
Follow this Question
Related Questions
How to differentiate between prefabs on collision. 1 Answer
Creating a Prefab - Javascript 2 Answers
Unity fire1 prefab in code 2 Answers
Getting a prefab with javascript FindAsset (Solved) 0 Answers
Resizeable gameObject array. 1 Answer