News › Forums › RAIN › General Discussion and Troubleshooting › Using Sendmessage and behavior trees
This topic contains 5 replies, has 2 voices, and was last updated by prime 2 months, 2 weeks ago.
-
AuthorPosts
-
December 16, 2022 at 5:32 am #34789
I am trying to send a message using a custom action and making a new class. Here is what I have so far in the class I created:
using UnityEngine; using System.Collections; using System.Collections.Generic; using RAIN.Action; using RAIN.Core; [RAINAction] public class TakeDamage : RAINAction { public override void Start(AI ai) { ai.gameObject.SendMessage("Damage", 1.0f, SendMessageOptions.DontRequireReceiver); base.Start(ai); } public override ActionResult Execute(AI ai) { return ActionResult.SUCCESS; } public override void Stop(AI ai) { base.Stop(ai); } }
I’ve tried using all of these instead of the sendmessage in the code.
GameObject.SendMessage: Returns error “An object reference is required to access non-static member”
Component.SendMessage: Returns error “An object reference is required to access non-static member”
gameObject.SendMessage: Returns error “The name `gameObject’ does not exist in the current context”
SendMessage: Returns error “The name `SendMessage’ does not exist in the current context”
ai.gameObject: returns error “RAIN.Core.AI’ does not contain a definition for `gameObject’ and no extension method `gameObject’ of type”
Any ways to do this successfully?December 16, 2022 at 9:07 am #34790ai.Body.SendMessage is what you want.
The “Body” of the AI is the parent game object that the AI controls.
December 16, 2022 at 11:43 am #34795Awesome, that fixed the error, but for some reason the send message is going to the wrong gameobject. I am very inexperienced in c# and don’t know what I am doing wrong. The sendmessage is sending a damage function to the enemy instead of the player for some reason. I originally got the function to work when I put it in a script attached to the enemy and used this format to get the message to the player(“Hero”):
void OnTriggerEnter(Collider other) { if (other.name == "Hero") other.SendMessage("Damage", 1.0f, SendMessageOptions.DontRequireReceiver); }
December 16, 2022 at 2:17 pm #34798So Hero is the player? I’m a little confused because your custom action is called TakeDamage. Is it applying damage to itself or to some other object?
December 17, 2022 at 8:14 am #34813It should be applying damage to some other object.
December 17, 2022 at 1:48 pm #34835ai.Body will be the game object of the AI itself, so you don’t want that. If you are applying damage to an object that you detected with a sensor, and you are setting the Form Variable through the sensor, then
GameObject tSensedObject = ai.WorkingMemory.GetItem<GameObject>("myFormVariable"); //whatever your variable name is, which should be used in the Form Variable field of the sensor tSensedObject.SendMessage("Damage", 1.0f, SendMessageOptions.DontRequireReceiver);
-
AuthorPosts
You must be logged in to reply to this topic.