Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Ekta-Mehta-D · May 07, 2013 at 09:46 AM · 2dgameobjectrandom

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();
 }
Comment
Add comment · Show 2
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 ahaykal · May 07, 2013 at 10:14 AM 0
Share

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..

avatar image Ekta-Mehta-D · May 07, 2013 at 10:29 AM 0
Share

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.. ??

1 Reply

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

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).

Comment
Add comment · Show 1 · 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 Ekta-Mehta-D · May 07, 2013 at 01:01 PM 0
Share

Thanx.. ur code useful for me..

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.

Follow this Question

Answers Answers and Comments

14 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

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


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