OnTriggerEnter triggering when not colliding anything, and vice versa

I’m using C# for this.

I have a hitbox that is being spawned on my characters hand. It is set to Is Trigger, has a rigidbody, and a script that looks for a couple things: A KillSelf notice, and a collision check to tell the PlayerScript what it collides with (Specifically, which object instance that has the script EnemyScriptCube on it that it collided with). I have another hitbox for normal attacks, and that one works perfect, but this one designed for grabs is not working properly. Actually, aside from a OnTriggerEnter check on the hitbo, it is precisely the same as the attack hitbox script.

The grab hitbox is behaving incredibly strangely. If it actually collides with anything, a wall, an enemy, or a breakable block, it does nothing, the collision check never even happens, which a Debug.Log tells me because it never appears in the console. However, and this is even weirder, if you are moving to the left and hit the grab key, the enemy box is instantly parented to the players hand, even if you are nowhere near it.

Here are the relevant pieces of code. If anyone could point out where I’m going wrong, it would be greatly appreciated.

First, the grabState of the playerScript:

if (state == playerState.grabState)
{

		Grabbox();
		
		
		if (controller.isGrounded)
		{
			if(controller.velocity.sqrMagnitude < 0.1) 
			{
				if (grabSub == grabSubState.NothingState)
				{
					if (!GrabAttempted)
					{
						animation.Stop ("IdlePose");
						animation.Stop ("RunPose");
						animation.Play("GrabAttemptPose");
						GrabAttempted = true;
					}
					if (!animation.IsPlaying("GrabAttemptPose") && GrabAttempted)
					{
						if (grabbedEnemy != null)
						{
							grabSub = grabSubState.grabIdleState;
							
						}
						else if (grabbedEnemy == null)
						{
							state = playerState.idleState;
							GrabbScript.KillSelf = true;
							grabSub = grabSubState.NothingState;
							grabbedEnemy.grabbed = false;
							GrabAttempted = false;
						}
						
					}
				}
			}
			else if (controller.velocity.sqrMagnitude > 0.1)
			{
				if (grabSub == grabSubState.NothingState)
				{
					if (!GrabAttempted)
					{
						animation.Stop ("IdlePose");
						animation.Stop ("RunPose");
						animation.Play("PouncePose");
						GrabAttempted = true;
					}
					if (!animation.IsPlaying("PouncePose") && GrabAttempted)
					{
						if (grabbedEnemy != null)
						{
							grabSub = grabSubState.grabIdleState;
							grabbedEnemy.grabbed = true;
						}
						else if (grabbedEnemy == null)
						{
							state = playerState.idleState;
							GrabbScript.KillSelf = true;
							grabSub = grabSubState.NothingState;
							grabbedEnemy.grabbed = false;
							GrabAttempted = false;
						}
					}
				}
			}
		}
		
		if (grabSub == grabSubState.grabIdleState)
		{
			//Play the right animation
			animation.CrossFade("GrabHoldPose");
			
			//Movement stuff
			moveDirection = new Vector3(0, controller.transform.position.y, Input.GetAxis("Horizontal"));
		    if (Input.GetAxis("Horizontal") > deadZone)
			{
				moveDirection = transform.TransformDirection(moveDirection);
			}
			if (Input.GetAxis("Horizontal") < deadZone)
			{
				moveDirection = transform.InverseTransformDirection(moveDirection);
			}
		    moveDirection *= playerSpeed;
			
			if (controller.velocity.sqrMagnitude > 0.1)
			{
				grabSub = grabSubState.grabMoveState;
				
			}
			
			
			
			if (GrabButtUp || !GrabButtHeld)
			{
				grabSub = grabSubState.grabEatState;
			}
				
			if (jumpButtDown && jumping!=true)
			{
				moveDirection.y = jumpSpeed;
				jumping = true;
				lastGroundedPos = controller.transform.position.y;
				gravity_on = false;
				
				
				moveDirection.y -= grav / 0.5f * Time.deltaTime;
				
				animation.Play("GrabJumpUpPose");
			}
			
		}
		
		if (grabSub == grabSubState.grabMoveState)
		{
			//Play the right animation
			animation.CrossFade("GrabRunPose");
			
			//Movement stuff
			moveDirection = new Vector3(0, controller.transform.position.y, Input.GetAxis("Horizontal"));
		    if (Input.GetAxis("Horizontal") > deadZone)
			{
				moveDirection = transform.TransformDirection(moveDirection);
			}
			if (Input.GetAxis("Horizontal") < deadZone)
			{
				moveDirection = transform.InverseTransformDirection(moveDirection);
			}
		    moveDirection *= playerSpeed;
			
			if (controller.velocity.sqrMagnitude < 0.1)
			{
				grabSub = grabSubState.grabIdleState;
				
			}
			
			if (GrabButtUp || !GrabButtHeld)
			{
				grabSub = grabSubState.grabEatState;
			}
			
			if (jumpButtDown && jumping!=true)
			{
				moveDirection.y = jumpSpeed;
				jumping = true;
				lastGroundedPos = controller.transform.position.y;
				gravity_on = false;
				
				
				moveDirection.y -= grav / 0.5f * Time.deltaTime;
				
				animation.Play("GrabJumpUpPose");
			}
		}
		
		if (!controller.isGrounded)
		{
			grabSub = grabSubState.grabJumpState;
			
			if (controller.isGrounded)
			{
				grabSub = grabSubState.grabIdleState;
			}
			if (GrabButtUp || !GrabButtHeld)
			{
				grabSub = grabSubState.grabJumpState;
			}
				
		}
		
		if ((grabbedEnemy != null) && grabSub == grabSubState.grabJumpState)
		{
			if(jumping == true && gravity_on == false &&(jumpButtUp || (controller.transform.position.y - lastGroundedPos) > jumpMax))
			{
				gravity_on = true;
			}
			
			if(jumping == true && gravity_on == true)
			{
				moveDirection.y -= grav* Time.deltaTime;
			}
				
			//Do it's animation
			animation.PlayQueued("GrabJumpHangPose",QueueMode.CompleteOthers);
			
			if (controller.isGrounded)
			{
				grabSub = grabSubState.grabIdleState;
			}
			if (GrabButtUp || !GrabButtHeld)
			{
				grabSub = grabSubState.grabJumpState;
			}
		}
		
		if ((grabbedEnemy != null) && (GrabAttempted) && (GrabButtUp || !GrabButtHeld))
		{
			grabSub = grabSubState.grabEatState;
		}
		
		if (controller.isGrounded && grabSub == grabSubState.grabEatState)
		{
			
			Vector3 acceleration = force / mass;
    		velocity += acceleration * Time.deltaTime; //Apply acceleration

			velocity -= Vector3.ClampMagnitude(velocity, 1) * drag * Time.deltaTime; //Apply drag
			
			moveDirection = (velocity * Time.deltaTime);
			force = Vector3.zero;
			
			animation.Stop("IdlePose");
			animation.Stop("RunPose");
			if (!eatingPlayed)
			{
				animation.Play("EatingPose");
				eatingPlayed = true;
			}
			animation.PlayQueued("IdlePose");
			
			if (!animation.IsPlaying("EatingPose"))
			{
				grabbedEnemy.eaten = true;
				GrabAttempted = false;
				GrabbScript.KillSelf = true;
				state = playerState.idleState;
				eatingPlayed = false;
				grabSub = grabSubState.NothingState;
			}
		}
			
		
	}

Next, the Grabbox method:

void Grabbox()
{
GameObject Grab = Instantiate(Resources.Load(“Grabb”)) as GameObject;
var GrabTra = Grab.transform;
Grab.transform.parent = Mob_GrabR;
GrabTra.localPosition = Vector3.zero;
GrabTra.localRotation = Quaternion.identity;
GrabTra.localScale = Vector3.one;

}

And finally, the actual script on the Grabb object:

using UnityEngine;
using System.Collections;

public class GrabbScript : MonoBehaviour {
public static bool KillSelf = false;
public PlayerScript player;

// Use this for initialization
void Start () 
{
	gameObject.layer = 8;
}

void Update ()
{
	if (KillSelf)
	{
		GameObject.Destroy(gameObject);
	}
}

void OnTriggerEnter(Collider collider) 
{
	Debug.Log ("Hit something...");
    EnemyScriptCube enemy = (EnemyScriptCube)collider.gameObject.GetComponent<EnemyScriptCube>();
    if (enemy != null) 
	{
        player.grabbedEnemy = enemy;
		Debug.Log ("Grabbed Enemy");
    }
}

}

Thanks for your time. I’ll do my best to answer any questions you have about this.

I figured out the issue. Turns out if you make a copy of a prefab and edit the name and change the script, it won’t behave properly. No idea why, but after remaking the Grabb object from scratch, everything works fine, despite having the exact same settings as before.

I’ll go ahead and mark this answered now that the problem has been solved.