News › Forums › RAIN › General Discussion and Troubleshooting › Custom Attack Script
Tagged: Attack, Custom Action, MonoBehavior, MonoBehaviour, Shooting
This topic contains 4 replies, has 2 voices, and was last updated by Iuxeayor 1 year, 7 months ago.
-
AuthorPosts
-
November 16, 2022 at 12:31 pm #34173
I am trying to make my AI begin shooting (very simple) upon seeing the player. I have the Behavior Tree working, and I know that the player is being detected because I also have the object set to chase the player when he is spotted; and that is working. The problem is somewhere in the Custom Action Script. I am basically trying to use the Custom Action Script to access a regular MonoBehaviour script that is attached to the same object that the AI is attached to, but it is childed under a couple of layers. The MonoBehavior script handles the shooting of the blaster and the Custom Action just makes the call to that function.
I am getting this error:
NullReferenceException: Object reference not set to an instance of an object
EnemyBlasterFireRAIN.Execute (RAIN.Core.AI ai) (at Assets/AI/Actions/EnemyBlasterFireRAIN.cs:27)
RAIN.BehaviorTrees.BTActionNode.Execute (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTConstraintNode.Execute (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTSelectorNode.Execute (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTParallelNode.Execute (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
RAIN.Minds.BasicMind.Think ()
RAIN.Core.AI.Think ()
RAIN.Core.AIRig.AIUpdate ()
RAIN.Core.AIRig.Update ()Here is my Custom Action Script:
using UnityEngine; using System.Collections; using System.Collections.Generic; using RAIN.Action; using RAIN.Core; [RAINAction] public class EnemyBlasterFireRAIN : RAINAction { EnemyBlasterFire fireBlasters; public override void Start(AI ai) { base.Start(ai); fireBlasters = ai.WorkingMemory.GetItem<EnemyBlasterFire>("EnemyBlasterFire"); } public override ActionResult Execute(AI ai) { fireBlasters = ai.WorkingMemory.GetItem<EnemyBlasterFire>("EnemyBlasterFire"); fireBlasters.Shoot(); return ActionResult.SUCCESS; } public override void Stop(AI ai) { base.Stop(ai); } }
And here is the script that the Custom Action is referencing.
using UnityEngine; using System.Collections; public class EnemyBlasterFire : BlasterFire { void Start() { Target = GameObject.FindGameObjectWithTag("Player"); } void Update () { } public void Shoot() { Rigidbody newBlast = Instantiate(rBlasterBolt, transform.position, transform.rotation) as Rigidbody; newBlast.AddForce(transform.forward * fVelocity, ForceMode.VelocityChange); Destroy(newBlast.gameObject, 3f); } void OnCollisionEnter(Collision col) { if(col.gameObject.tag == "Player") { healthScript = col.gameObject.GetComponent<Health>(); healthScript.ReduceHealth(DamagePoints); } } }
Is this not allowed? Is there a better way to do this? I know that generally speaking the EnemyBlasterFire script works because I am using the same basic code for the player’s blaster minus the player’s trigger button. Any help, suggestions, and comments are welcome.
Thank you,
-Iuxeayor ;>November 16, 2022 at 12:36 pm #34175instead of this:
ai.WorkingMemory.GetItem<EnemyBlasterFire>("EnemyBlasterFire");
try this:
ai.Body.GetComponentInChildren<EnemyBlasterFire>();
November 16, 2022 at 12:37 pm #34176Cool. Let me give that a try. ;>
November 16, 2022 at 12:50 pm #34178It seems that managed to get rid of the error. However, the MonoBehaviour script is not being executed.
November 16, 2022 at 1:57 pm #34179Nevermind. My Unity had crashed after I had updated the code - not sure why - and when I restarted it, my Custom Action Node had been removed from the BT and I didn’t notice. I added it back in and everything seems to be doing what it is supposed to.
Thanks, Prime!
-Iuxeayor ;> -
AuthorPosts
You must be logged in to reply to this topic.