News › Forums › RAIN › General Discussion and Troubleshooting › Instantiate inside custom action?
This topic contains 5 replies, has 3 voices, and was last updated by prime 1 month, 2 weeks ago.
-
AuthorPosts
-
January 2, 2023 at 6:56 am #34965
My enemy, when it is close to the player, makes appear another object, so I used a custom action in the IA, in the Memory of the IA I set a variable as GameObject called “instrument” and I put the object that is to appear, then the custom action there is only this code.
import RAIN.Action; import RAIN.Core; @RAINAction class attack_enemy extends RAIN.Action.RAINAction { function Start(ai:RAIN.Core.AI):void { super.Start(ai); Instantiate(instrument,Vector3(transform.position.x+2, transform.position.y -9, transform.position.z-10) , transform.rotation); } function Execute(ai:RAIN.Core.AI):ActionResult { return ActionResult.SUCCESS; } function Stop(ai:RAIN.Core.AI):void { super.Stop(ai); } }
But it gives me error
Unknown identifier: “Instantiate”
- This topic was modified 1 month, 3 weeks ago by Simsure.
January 2, 2023 at 7:13 am #34967Instantiate is a method of UnityEngine.Object class - which is parent of MonoBehaviour, and RAINAction is not a child of MonoBehaviour or any of Unity classes, so you can’t use Instantiate in it.
But you can use it in following way: UnityEngine.Object.Instantiate, because it’s public static method
- This reply was modified 1 month, 3 weeks ago by Astaror.
January 2, 2023 at 10:06 am #34969ok now works, thanks.
But to call the variable that I set in “memory” I have to use this code right?AI.WorkingMemory.GetItem
Then I did something like that.
var instrument: GameObject; instrument = ai.WorkingMemory.GetItem<GameObject>("instrument ");
But he says
Assets / AI / Actions / enemy_attack.js (13,53): BCE0164: Can not infer generic arguments for method ‘RAIN.Memory.RAINMemory.GetItem. <T> (String)’. Provide stronger type information through arguments, or Explicitly been the generic arguments.
- This reply was modified 1 month, 3 weeks ago by Simsure.
January 3, 2023 at 7:03 am #34974I believe the proper js syntax is
instrument = ai.WorkingMemory.GetItem.<GameObject>("instrument");
January 3, 2023 at 10:59 am #34978It gave me the error as ” transform” , I thought it was the same problem as before and I tried to solve it this way, I was right ?
I do not think because nothing happens during the game !
UnityEngine.Object.Instantiate(instrument,Vector3(UnityEngine.Object.transform.position.x+2, UnityEngine.Object.transform.position.y -9, UnityEngine.Object.transform.position.z-10) , UnityEngine.Object.transform.rotation);
- This reply was modified 1 month, 3 weeks ago by Simsure.
January 6, 2023 at 9:17 am #34995You want something more like this:
UnityEngine.Object.Instantiate(instrument,Vector3(ai.Body.transform.position.x + 2, ai.Body.transform.position.y - 9, ai.Body.transform.position.z - 10) , ai.Body.transform.rotation);
-
AuthorPosts
You must be logged in to reply to this topic.