Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Two objects with the same script interfering with each other

Discussion in '2D' started by Hemessfell, Jun 20, 2020.

  1. Hemessfell

    Hemessfell

    Joined:
    Aug 3, 2019
    Posts:
    46
    I have this object that when I collide with it, it plays an animation and sets the player as its parent, pretty basic stuff. But for some reason, when I have two of the same object active at the same time in the scene, this happens: https://hamassfall.tumblr.com/post/621398774625058816

    This is the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlankBehaviour : MonoBehaviour
    6. {
    7.     private Animator anim;
    8.  
    9.     private void Awake()
    10.     {
    11.         anim = GetComponent<Animator>();
    12.     }
    13.  
    14.     private void OnCollisionEnter2D(Collision2D other)
    15.     {
    16.         if (other.gameObject.CompareTag("Player"))
    17.         {
    18.             anim.SetTrigger("isStomped");
    19.             other.transform.SetParent(transform, true);
    20.          
    21.         }
    22.     }
    23.  
    24.     private void OnCollisionExit2D(Collision2D other)
    25.     {
    26.         if (other.gameObject.CompareTag("Player"))
    27.         {
    28.             anim.SetTrigger("isStomped");
    29.             other.transform.SetParent(null);
    30.         }
    31.     }
    32. }
    33.  
    Does anyone know what's happening?
     
    Last edited: Jun 20, 2020
  2. Neomedus

    Neomedus

    Joined:
    May 14, 2020
    Posts:
    47
    It looks like the only possible problem would be with you setting the parent. I would debug.log the parent name of your character.