• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by El Maxo · Mar 13, 2014 at 12:12 PM · javascriptprefabmissions

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();
 //    }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image El Maxo · Mar 13, 2014 at 02:20 PM 0
Share

can anyone help :/?

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer Wiki

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;


Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image El Maxo · Mar 13, 2014 at 09:36 PM 0
Share

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?

avatar image Jeremy2300 · Mar 13, 2014 at 09:57 PM 0
Share

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.

avatar image El Maxo · Mar 13, 2014 at 10:15 PM 0
Share

I will add my idea below

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity fire1 prefab in code 2 Answers

Getting a prefab with javascript FindAsset (Solved) 0 Answers

How to differentiate between prefabs on collision. 1 Answer

Creating a Prefab - Javascript 2 Answers

Reference a prefab through a variable or SendMessage 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges