OnTrigger not return null ?

Hi all… I have problem with onTrigger. in my game, i have player and enemy. When player collider trigger with enemy, player will aim at enemy and if player shoot, enemy will disable… the problem is after enemy disable, player still aim at enemy position. I need my player to stop aim if enemy disable…

here is my code…

 public GameObject target = null;
 private bool aimTarget;

 void Update () {
	if (aimTarget == false) 
	{
		target = null;
	}
  }

  void OnTriggerStay(Collider col){
	if (col.gameObject.tag == "Teddy") 
	{
		aimTarget = true;
		target = col.gameObject;
	}
}

  void OnTriggerExit(Collider col){
	if (col.gameObject.tag == "Teddy") 
	{
		aimTarget = false;
	}
}

How to make “target” return null after enemy disable?
I’m new in unity and c#. Hope someone can clarify…

First: is Player with trigger collider motionless? If it moving, it collider can’t work as trigger.
Second: disabling of enemy don’t rise OnTriggerExit event. You must do it manually or use another way of clearing aimTarget

I think it is not all script? I can’t see where you proceed user’s input. And how script make decision is player shoot the target or migging? Or “one shot - one kill”? :slight_smile: Last of all the disabling of enemy can be made as the result of shooting. If it was successful, you can disable enemy at this procedure. How you proceed OnTriggerEnter()? Is enemy disable until game over or spawn (enabe) somewhere after sometime? It’s problematically to give any advice without seeing all script code. Can you show it?

hi Yulka… This is the full script…

for player…

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace UnityStandardAssets.CrossPlatformInput 
{

public class Bazooka : MonoBehaviour 
{

	protected Animator animator;

    public GameObject target = null;	
    public GameObject leftHandle = null;
    public GameObject rightHandle = null;
    public GameObject bazoo = null;
	public GameObject bullet = null;
	public float bulletSpeed = 20f;
	public GameObject spawn = null;

	private Transform spawnT;
	private GameObject fireEffect;
	private GameObject shootSound;
	private PoolingSystem poolingSystem;
	private bool aimTarget;

	void Start () 
	{
		poolingSystem = PoolingSystem.Instance;
		animator = GetComponent<Animator>();
		spawnT = spawn.transform;
		fireEffect =  poolingSystem.InstantiateAPS("ShootEffect", spawnT.position ,spawnT.rotation, spawn);			
		shootSound =  poolingSystem.InstantiateAPS("ShootSound", spawnT.position, spawnT.rotation, Camera.main.GetComponent<Camera>().gameObject);
	}

	void Update () 
	{
		if (aimTarget == false) 
		{
			target = null;
		}

		if (animator)
		{
            animator.SetFloat("Aim",1 , .1f, Time.deltaTime);

            float aim = animator.GetFloat("Aim");
            float fire = animator.GetFloat("Fire");

			if (CrossPlatformInputManager.GetButton("Fire1") && fire < 0.01 && aim > 0.99)	// to shoot bullets
			{
				animator.SetFloat("Fire",1);
               	{
					GameObject bullets = poolingSystem.InstantiateAPS ("Bullet", spawn.transform.position , Quaternion.Euler(0, 0, 0)) as GameObject;

                	Rigidbody rb = bullets.GetComponent<Rigidbody>();

                 	if (rb != null)
                	{
						rb.velocity = spawn.transform.TransformDirection(Vector3.forward * bulletSpeed);
                   	}
					if(bullets == null)
						return;

					fireEffect.PlayEffect(30);						
					shootSound.PlaySound();
              	}
          	}
            else
            {
               animator.SetFloat("Fire",0, 0.1f, Time.deltaTime);
            }

			float h = CrossPlatformInputManager.GetAxis("Horizontal");
			float v = CrossPlatformInputManager.GetAxis("Vertical");
			
			animator.SetFloat("Speed", h*h+v); //original ("Speed", h*h+v*v)
            animator.SetFloat("Direction", h, 0.25f, Time.deltaTime);
          }   		  
	}

    void OnAnimatorIK(int layerIndex)
    {
        float aim = animator.GetFloat("Aim");

		if (bazoo != null)
		{
			float fire = animator.GetFloat("Fire");
			Vector3 pos = new Vector3(0.195f, -0.0557f, -0.155f);
			Vector3 scale = new Vector3(0.2f, 0.8f, 0.2f);
			pos.x -= fire * 0.2f;
			scale = scale * aim;
			bazoo.transform.localScale = scale;
			bazoo.transform.localPosition = pos;
		}

        // solve lookat and update bazooka transform on first il layer
        if (layerIndex == 0)
        {
            if (target != null)
            {
                Vector3 targetA = target.transform.position;

                targetA.y = targetA.y + 0.2f * (targetA - animator.rootPosition).magnitude;

                animator.SetLookAtPosition(targetA);
                animator.SetLookAtWeight(aim, 0.5f, 0.5f, 0.0f, 0.5f);   	 
            }
        }

        // solve hands holding bazooka on second ik layer
        if (layerIndex == 1)
        {
            if (leftHandle != null)
            {
                animator.SetIKPosition(AvatarIKGoal.LeftHand, leftHandle.transform.position);
                animator.SetIKRotation(AvatarIKGoal.LeftHand, leftHandle.transform.rotation);
                animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, aim);
                animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, aim);
            }

            if (rightHandle != null)
            {
                animator.SetIKPosition(AvatarIKGoal.RightHand, rightHandle.transform.position);
                animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandle.transform.rotation);
                animator.SetIKPositionWeight(AvatarIKGoal.RightHand, aim);
                animator.SetIKRotationWeight(AvatarIKGoal.RightHand, aim);
            }
        }
    }

	void OnTriggerStay(Collider col)
	{
		if (col.gameObject.tag == "Teddy") 
		{
			aimTarget = true;
			target = col.gameObject;
		}
	}

	void OnTriggerExit(Collider col)
	{
		if (col.gameObject.tag == "Teddy") 
		{
			aimTarget = false;
		}
	}
}
}

this is enemy…

using UnityEngine;
using System;
using System.Collections;

[RequireComponent(typeof(Animator))]  

public class Bear : MonoBehaviour {

public float AvatarRange = 25;

protected Animator avatar;

private float SpeedDampTime = .25f;	
private float DirectionDampTime = .25f;	
private Vector3 targetPosition;

void OnEnable ()
{
	targetPosition = transform.localPosition;
	GetComponent<Collider> ().isTrigger = false;
}

void Start () 
{
	avatar = GetComponent<Animator>();
}

void Update () 
{
}

void OnCollisionEnter(Collision collision)
{
	switch(collision.gameObject.tag)
	{
		case "Player":
			Debug.Log ("Player");
			break;

		case "Vegetation":
			Debug.Log ("Vegetation");
			break;

		case "Bullet":
			if (avatar != null)
        	{
				var currentState = avatar.GetCurrentAnimatorStateInfo(0);
				var nextState = avatar.GetNextAnimatorStateInfo(0);
				if (!currentState.IsName("Base Layer.Dying") && !nextState.IsName("Base Layer.Dying"))
					avatar.SetBool("Dying", true);

				StartCoroutine(LateCall());
			}
		break;
    }        
}

IEnumerator LateCall()
{	
	yield return new WaitForSeconds(6);
		GetComponent<Collider> ().isTrigger = true;

	yield return new WaitForSeconds(1);
		gameObject.DestroyAPS();
}
}

i’m edit enemy script, because too much…
Enemy disable until spawn (enabe)… using pooling system…