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 /
avatar image
0
Question by GeorgeGiap · May 22, 2018 at 05:09 PM · gameobjectscript.destroy

One script for multiple enemy objects problem

Here is my problem. I am making a 2D platformer super mario bros clone. I have 2 enemies in the scene and one script is attached to both of them. If i step on one of them the other one is getting destroyed. What can i do to solve this problem?

Here is the EnemyController script that extends the RaycastController class:

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     
     public class EnemyController : RaycastController
     {
         public float moveSpeed = -30;  
         [HideInInspector]
         public Vector2 velocity = Vector2.zero;
     
         private float gravity = -30;
         private float directionX = 1;
         private float directionY;
         private Rigidbody2D rb2d;
         private Animator animator;
         private AudioManager audioManager;
     
         // Use this for initialization
         void Start()
         {
             Time.timeScale = 1; // restore time scale from before pause
             Debug.Log("THIS particular script is on " + this.name);
             base.Start();
             rb2d = GetComponent<Rigidbody2D>();
             animator = GetComponent<Animator>();
             audioManager = FindObjectOfType<AudioManager>();
     
             UpdateRaycastOrigins();
         }
     
         // Update is called once per frame
         void Update()
         {
             UpdateRaycastOrigins();
             HorizontalCollisions();
             VerticalCollisions();
             rb2d.velocity = new Vector2(velocity.x, -velocity.y);
         }
     
         void HorizontalCollisions()
         {
             float rayLength = 1.5f;
     
             for (int i = 5; i < horizontalRayCount - 42; i++) 
             {
                 Vector2 rayOriginLeft = raycastOrigins.bottomLeft;
                 Vector2 rayOriginRight = raycastOrigins.bottomRight;
                 rayOriginLeft += Vector2.up * (horizontalRaySpacing + 1 * i + skinWidth);
                 rayOriginRight += Vector2.up * (horizontalRaySpacing + 1 * i + skinWidth);
                 RaycastHit2D hitLeft = Physics2D.Raycast(rayOriginLeft, Vector2.left, rayLength, collisionMask);
                 RaycastHit2D hitRight = Physics2D.Raycast(rayOriginRight,Vector2.right, rayLength , collisionMask);
                 Debug.DrawRay(rayOriginLeft, Vector2.left, Color.red);
                 Debug.DrawRay(rayOriginRight, Vector2.right , Color.red);
     
                 if (hitLeft)
                 {
                     directionX = -directionX;
                     rayLength = hitLeft.distance;
                     if (hitLeft.collider.tag == "Player")
                     {
                         if (!audioManager.audio.isPlaying)
                             audioManager.PlayAudio("Die");
                         StartCoroutine(Pause());
                         PlayerLose();
                     }
                 }
                 if (hitRight)
                 {
                     directionX = -directionX;              
                     rayLength = hitRight.distance;
                     if (hitRight.collider.tag == "Player")
                     {
                         if (!audioManager.audio.isPlaying)
                             audioManager.PlayAudio("Die");
                         StartCoroutine(Pause());
                         PlayerLose();
                     }
                 }
                 velocity.x = moveSpeed * directionX;
             }
         }
     
         void VerticalCollisions()
         {
             float rayLength = 1.5f;
     
             for (int i = 0; i < verticalRayCount-39; i++)
             {
     
                 Vector2 rayOriginDown = raycastOrigins.bottomLeft;
                 Vector2 rayOriginUp = raycastOrigins.topLeft;
                 rayOriginDown += Vector2.right * (verticalRaySpacing + 1 * i + skinWidth);
                 rayOriginUp += Vector2.right * (verticalRaySpacing + 1 * i + skinWidth);
                 RaycastHit2D hitDown = Physics2D.Raycast(rayOriginDown, Vector2.down, rayLength , collisionMask);
                 RaycastHit2D hitUp = Physics2D.Raycast(rayOriginUp, Vector2.up, rayLength , collisionMask);
                 Debug.DrawRay(rayOriginDown, Vector2.down, Color.red);
                 Debug.DrawRay(rayOriginUp, Vector2.up , Color.red);
                
                 if (hitDown)
                 {
                     velocity.y = 0;          
                     rayLength = hitDown.distance;   
                     if(hitDown.collider.tag == "Player")
                     {
                         if(!audioManager.audio.isPlaying)
                             audioManager.PlayAudio("Die");
                         StartCoroutine(Pause());
                         PlayerLose();
                     }
                 }
                 else
                 {
                     velocity.y -= gravity * Time.deltaTime;
                 }
                 if (hitUp)
                 {
                     if(hitUp.collider.tag == "Player")
                     {
                         velocity.x = 0;
                     }
                     velocity.y = 0;
                     rayLength = hitUp.distance;
                 }
             }
         }
     
         public void EnemyDeath()
         {
             animator.Play("Dead");
             animator.SetBool("Crushed", true);
             audioManager.PlayAudio("Squish");
             Destroy(this.gameObject);
             Destroy(this.rb2d);
             Destroy(this.collider);       
             Destroy(this.gameObject, 1f);     
         }
     
         IEnumerator Pause()
         {
             Time.timeScale = 0; // pause
             yield return null; 
         }
     
         void PlayerLose()
         {
             Application.LoadLevel("Lose");
         }
     }
 `
 And here is the RaycastController script:
 
 `
 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof (BoxCollider2D))]
 public class RaycastController : MonoBehaviour {
 
     public struct RaycastOrigins
     {
         public Vector2 topLeft, topRight;
         public Vector2 bottomLeft, bottomRight;
     }
     public RaycastOrigins raycastOrigins;
     public LayerMask collisionMask;
     protected BoxCollider2D collider;
     protected const float skinWidth = .015f;
     protected int horizontalRayCount;
     protected int verticalRayCount;
     protected float horizontalRaySpacing;
     protected float verticalRaySpacing;
 
     private const float dstBetweenRays = .25f;
 
 
     public virtual void Awake() {
         collider = GetComponent<BoxCollider2D> ();
     }
 
     public virtual void Start() {
         CalculateRaySpacing ();
     }
 
     public void UpdateRaycastOrigins() {
         Bounds bounds = collider.bounds;
         bounds.Expand (skinWidth * -2);
         
         raycastOrigins.bottomLeft = new Vector2 (bounds.min.x, bounds.min.y);
         raycastOrigins.bottomRight = new Vector2 (bounds.max.x, bounds.min.y);
         raycastOrigins.topLeft = new Vector2 (bounds.min.x, bounds.max.y);
         raycastOrigins.topRight = new Vector2 (bounds.max.x, bounds.max.y);
     }
     
     public void CalculateRaySpacing() {
         Bounds bounds = collider.bounds;
         bounds.Expand (skinWidth * -2);
 
         float boundsWidth = bounds.size.x;
         float boundsHeight = bounds.size.y;
         
         horizontalRayCount = Mathf.RoundToInt (boundsHeight / dstBetweenRays);
         verticalRayCount = Mathf.RoundToInt (boundsWidth / dstBetweenRays);
         
         horizontalRaySpacing = bounds.size.y / (horizontalRayCount - 1);
         verticalRaySpacing = bounds.size.x / (verticalRayCount - 1);
     }
 }








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

· Add your reply
  • Sort: 
avatar image
0

Answer by tormentoarmagedoom · May 22, 2018 at 10:05 PM

Why are you using Raycast?

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 GeorgeGiap · May 23, 2018 at 07:20 AM

Why not? This is just a project for learning purposes and nothing else. Is there any way that i can solve this problem? Or i would be grateful if you can suggest any other way of doing it. If you know please share it with me. Thanks.

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

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

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

Instantiated bullet wont destroy 1 Answer

My GameObject remains inGame but is destroyed in the editor (no GameObject on left side/in hierarchy) 0 Answers

How do I access an object script variable that is in a 2D array? 0 Answers

Keep GameObject destroyed on returning to the scene 1 Answer

Canon Ball shooting with Instiate 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