Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Apr 14, 2017 at 11:32 AM by CosmicSloth for the following reason:

Other

avatar image
0
Question by CosmicSloth · Apr 14, 2017 at 09:19 AM · enemy airespawnenemy spawnrespawning

Respawing Enemy After GameObject is Destroyed

At the moment, I have a simple script that destroys the enemy when it collides with my weapon:

 using UnityEngine;
 using System.Collections;
 
 public class killenemy : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.tag == "enemy")
         {
             Destroy (other.gameObject);
         }
 
     }
 
 }
 

How can I change this so that it respawns the enemy after 3 seconds at the same starting location with the same movement script attached that follows a set of waypoints around the map? I fear that destroying the gameobject itself will get rid of the movement script completely. Do I need a 'spawner' or can I do each enemy individually? I'll only have around 3 enemies in total.

Movement Script:

 #pragma strict
  
  var waypoint : Transform[];            
  var patrolSpeed : float = 3;       
  var loop : boolean = true;            
  var dampingLook = 6.0;               
  var pauseDuration : float = 0;        
  
  private var curTime : float;
  private var currentWaypoint : int = 0;
  private var character : CharacterController;
  
  function Start(){
  
      character = GetComponent(CharacterController);
  }
  
  function Update(){
  
      if(currentWaypoint < waypoint.length){
          patrol();
          }else{    
      if(loop){
          currentWaypoint=0;
          } 
      }
  }
  
  function patrol(){
  
          var target : Vector3 = waypoint[currentWaypoint].position;
          var moveDirection : Vector3 = target - transform.position;
  
      if(moveDirection.magnitude < 0.5){
          if (curTime == 0)
              curTime = Time.time; 
          if ((Time.time - curTime) >= pauseDuration){
              currentWaypoint++;
              curTime = 0;
          }
      }else{        
          var rotation = Quaternion.LookRotation(target - transform.position);
          transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);
          character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);
      }    
  }
  
Comment
Add comment
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

2 Replies

  • Sort: 
avatar image
1

Answer by Laseph · Apr 14, 2017 at 09:45 AM

If you are using a prefab to make your enemy then it should be no problem because it always has the components you attached to it. For the spawning of enemy's I would indeed make a spawner gameobject with a script. You would need to have an event for when the enemy has died, the spawner could see it and know that there's no longer three enemy's in the playing field. Then you would have a timer of three seconds before you instantiate the new enemy.

  • I would personally not call destroy directly and instead would have a Die method in the enemy class in which you can destroy it and do other things (Much more flexible, this would also be the method where you would call the event)

  • If you don't know about events in c# you should take a look because they are really useful (you could also take a look at UnityEvents, it's practically the same thing and it also works with the inspector)

If you need me to elaborate just say so.

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 CosmicSloth · Apr 14, 2017 at 10:08 AM 0
Share

I don't have a prefab for any enemy. I just have the one enemy so far to test on and I just placed it in the location I want it to start from as it plays the movement script when the game starts and moves around the points I set so the 3 enemies (when I drop them in) will already be present when the game starts, I didn't think I needed to spawn them right off the bat if they are already in the position I set.

I don't know what Die methods are or events. I'm already way in over my head with this, I only posted the question as reading the manuals don't make anything clearer. I'm trying to do one thing at a time but everything's so confusing and now I'm on a time limit.

avatar image
0

Answer by Cuttlas-U · Apr 14, 2017 at 10:55 AM

hi; u can create a function in the enemy script that change its position to where u like;

for example public void ResetPosition();

then u don't destroy it just call this function to reset the position and other changes that should be done to the enemy u do in that function;

other.gameObject.GetComponent(Movement ).ResetPosition();

/////////////////////////////////////////////

////////////// the better idea is not to destroy it at the first place; 1 good thing and optimized way is to save the objects that u don't want any more in a pool system; then u can get back any thing u want again from the pool;

u can search for pooling system there are good tutorials about it in the youtube;

but if u want to do it simple u can just dissactive it and not destroy by using :

 other.gameObject.SetActive(false);

this way u don't need to do any thing ; also u need a manager to holde all of your enemy's in an array at the start of the game;

then u send a request to the manager script and ask for an enemy object; the manager script look through the array if any enemy object is dissactive and activate it for u ; /////////////////

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

Follow this Question

Answers Answers and Comments

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

fpscontroller doesnt respawn (maybe transform.position issue?) 0 Answers

When I activate the animator of the enemy and play it spawns at the corner of the map and don't move 0 Answers

i did a spawn script but when 2 identical enemies spawn and one attacks first, the other stops and starts the attacking animation too 1 Answer

Making an enemy go back to its spawn after losing target 0 Answers

How to make all enemies attack after spawning? Not only 1. 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