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 Karsnen_2 · Dec 07, 2011 at 07:41 AM · javascriptprefabdestroyenemyspawn

Destroy and Spawn an Enemy

Hello,

I am working on a

  1. Side scroller ( meant that the view is 2D) - only on X-Y plane. Z always '0'.

  2. The canon has to be placed on the left most corner and fire in a Projectile Fashion

Game Objects :

I have placed a script called as "enemypos" on an empty gameobject. Then there is another prefab called as the "enemy" which is the enemy.

Code:


The enemypos script is as follows :

 var projscript : projectile;
 var ene_pos: Transform;
 var pos : float;
 
 
 function Awake()
 {
     e_spawn();
 }
 
 
 function Update ()
 {
     
 
 }
 
 
 function e_spawn()
 {
     pos = projscript.spawn();
     var position: Vector3 = Vector3(pos, 1, 0);
     var enemy = Instantiate(ene_pos, position, Quaternion.identity);
 }


The spawn() function is as follows: (is called from another script)

  function spawn()
     {
         if(Random.Range(0,10)<7)
             return Random.Range(-18,10);
         else
             return Random.Range(10,21);
         
     }



Code Explanation:

The function e_spawn() is used to instantiate the enemy prefab.

Request:

My humble request is how to destroy and instantiate the same enemy along the x axis.

THANK YOU in advance

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 Karsnen_2 · Dec 07, 2011 at 05:48 PM 0
Share

Fellow Unity Users - $$anonymous$$y original question and it's explanation was wrong but now I have edited it.

Could someone kindly help me on this?

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by BiG · Dec 07, 2011 at 08:02 AM

If I understand well, in the function spawn() you randomize the point on which the new enemy will appears, and you wanna modify that script. If so:

  var enemy : GameObject;
  var y = 0 //this is a constant value, I think...
 
  function spawn()
     {
         if(Random.Range(0,10)<7)
            newX = Random.Range(-18,10);
         else
            newX = Random.Range(10,21);
         
         Instantiate(enemy, Vector3(newX,y,0), Quaternion.identity);
     }

I think that it could be a solution for your problem.

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 Karsnen_2 · Dec 07, 2011 at 08:24 AM 0
Share

Sorry mate - Really sorry I messed up things. I was so confused that I made something else on my Question. I should not have done that. I have changed the question right now. If it was not you, I do not think I could have noticed it. I hope I am clear now.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to spawn a 'boss' after all enemies defeated and then kill that 'boss'? 1 Answer

Check if child exists and instantiate as child 1 Answer

Help have script know how many enemies are in the world 1 Answer

Destroying a prefab on collision with a cube? 1 Answer

Error: Instantiated Enemies don't get hit 2 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