While a NPC is walking...and it watches an Enemy NPC....

Jeremy

New member
Hello...Excuse Me...I need help...I am a begginer..

I want to make a game like this...

While an NPC is walking...and suddenly it watches an Enemy NPC....

The NPC start to Attck the Enemy NPC, and when the Enemy NPC is dead,

a NPC start to walk again..


But In Unity Editor...with These Script...Suddenly game stops,

when the EnemyNPC is dead, the gmae stops..

and the error message is " target Object is destoyed, buy you are still try to access it."(=WithinSight.script)

Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;

public class MoveTowards : Action
{

public float speed = 3.5f;

public SharedTransform target;

public Transform curTransform;

public GameObject bulletTerra;

public GameObject bulletPosTerra;

Rigidbody rigid;

UnityEngine.AI.NavMeshAgent nav;

public override void OnAwake()
{
rigid = GetComponent<Rigidbody>();

nav = GetComponent<UnityEngine.AI.NavMeshAgent>();
}

public override TaskStatus OnUpdate()
{
if (Vector3.SqrMagnitude(transform.position - target.Value.position) < 7f)
{
transform.position = curTransform.position;

StartCoroutine(Attack());

return TaskStatus.Success;

}
transform.position = Vector3.MoveTowards(transform.position, target.Value.position, speed * Time.deltaTime);

this.transform.LookAt(target.Value);

curTransform = transform;

return TaskStatus.Running;
}

IEnumerator Attack()
{
GameObject instantBullet = GameObject.Instantiate(bulletTerra, bulletPosTerra.transform.position, transform.rotation);


Rigidbody rigidBullet = instantBullet.GetComponent<Rigidbody>();

rigidBullet.velocity = transform.forward * 50;

yield return new WaitForSeconds(0.5f);

}
}
 
Last edited by a moderator:
Please use [ code ] tags so it's easier to read your script. I've edited your post with this formatting.

With that said, what does your tree look like?
 
As Justin said it'd be best to show your tree, so it can be better assessed. I would assume that WithinSight has not been aborted.
 
Top