I am trying to check if a Trigger hits an object from the object itself, but have been running into some issues, if the trigger is a trigger, then it seems to be ignored, and it is causing some problems. I am working in 2D, and my script currently looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HealthAttack : MonoBehaviour
{
public float health;
public float[] attackdamages;
int Attack;
public GameObject Attacked;
public GameObject Dead;
//Stand in function type because it is the closest thing I found to what I am looking for
private void OnTriggerEnter2D(Collider2D collision)
{
Debug.Log("Attacked!");
GameObject Attacker = collision.gameObject.transform.parent.gameObject.transform.parent.gameObject;
Debug.Log(Attacker.name);
}
}
if everything worked, the trigger would intersect with the collider on the object with this script, and then would get the parent of the parent (either my player or an enemy), where I could then make some more stuff happen
private void OnTriggerEnter2D(Collider2D collision) is how you detect triggers, whichever object has the script on it for detecting the collision will detect the collision. I’m not sure what issue you are having… in this line GameObject Attacker = collision.gameObject.transform.parent.gameObject.transform.parent.gameObject; you are getting the parent of the parent, but you need to show a pic of the object in the hierarchy for me to see if you are getting the right object with that code. A side note, using gameObject.transform is redundant. Especially here: parent.gameObject.transform parent is a transform already, so you are taking the reference to transform from the reference to gameObject to get the transform of a transform. You could go collision.transform.parent.parent.gameObject instead. What sort of other things do you want to do? If you want to access methods on the gameObject you have to have a reference to the script component that contains the methods you want. LIke if your attacker is a Enemy class that contains a method Attack(), then you would have to go Enemy Attacker = collision.gameObject.transform.parent.gameObject.transform.parent.gameObject.GetComponent(); I can help more with more info.