playMaker

Author Topic: Two identical "Create Object" actions, different behavior  (Read 4137 times)

magritte

  • Playmaker Newbie
  • *
  • Posts: 12
Two identical "Create Object" actions, different behavior
« on: May 28, 2014, 07:20:41 PM »
I posted this to bug reports, but it occurred to me that maybe I should post it here as well in case the mistake is mine and not PM's. Anybody ever experienced something like this? I'm using v1.7.7.

I have an instantiation of a prefab (gun). Attached to the gun's shoot state are two identical "Create Object" actions: create bullet (from a prefab) at spawn point gun.

When the gun goes into the shoot state, one bullet gets created at the location of the gun instance, one gets created at the location of the gun prefab. I am absolutely sure these actions are identical.

It's always the first bullet that spawns at the gun prefab while the second spawns at the gun instance. If I uncheck the first bullet create, the second will still spawn at the instance. But bizarrely, if I actually delete the first bullet create, the remaining bullet now spawns at the prefab.

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Two identical "Create Object" actions, different behavior
« Reply #1 on: May 28, 2014, 07:39:46 PM »
Quote
It's always the first bullet that spawns at the gun prefab

What do you mean by "at the gun prefab"?

Can you post a screenshot of the action stack? Or better yet, a quick video capture of the behavior you're seeing...

magritte

  • Playmaker Newbie
  • *
  • Posts: 12
Re: Two identical "Create Object" actions, different behavior
« Reply #2 on: May 28, 2014, 07:42:26 PM »
I mean that the bullet spawns at the transform location for the gun prefab rather then the transform of the gun instance. I can attempt to take a video.
« Last Edit: May 28, 2014, 07:43:59 PM by magritte »

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Two identical "Create Object" actions, different behavior
« Reply #3 on: May 28, 2014, 07:47:51 PM »
Not sure what's going on. I'd need to look at your setup.

Have you tried copy/pasting the Create Object action? Maybe they are setup differently (sometimes GameObject labels look the same but point to different objects).

Also, the new Top Down Shooter tutorials on our YouTube channel do exactly this, spawning a bullet from a spaceship. You could check that out and see if your setup is different...

1982

  • Junior Playmaker
  • **
  • Posts: 71
Re: Two identical "Create Object" actions, different behavior
« Reply #4 on: May 29, 2014, 03:26:27 AM »
It sounds you have lost Prefab-references at some point in your logic. You should save any reference-required Prefabs into a variable and refer to that.

magritte

  • Playmaker Newbie
  • *
  • Posts: 12
Re: Two identical "Create Object" actions, different behavior
« Reply #5 on: May 29, 2014, 10:30:35 PM »
As requested, here's a video. Please note that I only referred to the prefabs as "gun" and "bullet" in my OP because I thought it would be more illustrative of the problem. The actual prefabs are called "player" and "sword".


It's not shown in the video, but the only actions attached to the sword prefab are for it to destroy itself.
« Last Edit: May 30, 2014, 11:31:00 AM by magritte »

1982

  • Junior Playmaker
  • **
  • Posts: 71
Re: Two identical "Create Object" actions, different behavior
« Reply #6 on: May 31, 2014, 03:06:38 PM »
I watched your video, here's what is going on:

When you create the sword, it takes the spawn position from player Prefab. The position is the transform that is saved to the Prefab. You don't want that to happen, as Gameobjects and Prefabs should always refer to any current scene objects.

You should put action to your player prefab which saves itself into a Gameobject variable (preferably global var). Now when you create your sword at the runtime, you use that variable as spawn point.

You must also set the sword as a child of the player gameobject using "Set Parent", otherwise it won't follow the player.

« Last Edit: May 31, 2014, 03:08:12 PM by 1982 »

magritte

  • Playmaker Newbie
  • *
  • Posts: 12
Re: Two identical "Create Object" actions, different behavior
« Reply #7 on: June 01, 2014, 04:20:15 PM »
I watched your video, here's what is going on:

When you create the sword, it takes the spawn position from player Prefab. The position is the transform that is saved to the Prefab. You don't want that to happen, as Gameobjects and Prefabs should always refer to any current scene objects.

You should put action to your player prefab which saves itself into a Gameobject variable (preferably global var). Now when you create your sword at the runtime, you use that variable as spawn point.

You must also set the sword as a child of the player gameobject using "Set Parent", otherwise it won't follow the player.

Thank you for the response. I knew that one sword was spawning at the prefab (and I'll follow your suggestion to fix it), but I'm still confused as to why the other sword spawns at the instance. Any ideas there?

Also, your suggestion works for the player since there's only going to be one instance, but how might I handle the same situation when I'm creating multiple enemies from prefabs? There won't be one global variable to reference.

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Two identical "Create Object" actions, different behavior
« Reply #8 on: June 03, 2014, 01:12:57 PM »
This can get tricky in Unity. Most of the time you want to reference scene objects in actions. For example you can use Get Owner to get the scene object that the FSM is running on, or Find Child to find an attachment point etc.

However you can also reference child objects in a prefab. For example, if you have a player prefab, with a sword attachment point as a child, you should be able to specify the sword attachment point in any scripts/actions running on the player prefab. When you spawn the player prefab the scripts/actions should find the correct sword attachment point in the scene.

In your example, I'm not sure why it works sometimes, maybe Unity is doing some behind the scenes work to find the correct prefab instance. I would need to look at the project or try to repro here. But I wouldn't rely on this behaviour! You should either find the scene object or point to a child of the spawned prefab as described above.

Does that make sense?

magritte

  • Playmaker Newbie
  • *
  • Posts: 12
Re: Two identical "Create Object" actions, different behavior
« Reply #9 on: June 03, 2014, 01:42:45 PM »
It definitely makes sense Alex, thanks. Is there a good practice for how to handle this when you have multiple copies of a prefab? For example, several of the same enemy type onscreen, all of which are shooting fireballs at the player?
« Last Edit: June 03, 2014, 01:45:02 PM by magritte »

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Two identical "Create Object" actions, different behavior
« Reply #10 on: June 03, 2014, 02:11:20 PM »
Yeah, if you use Get Owner or Get Child on the enemy prefab it will get the proper scene objects. E.g., Get Owner will get the specific scene instance of the prefab, and Get Child will get a child of that scene instance.

Avoid global variables for these kinds of things! Each scene instance should have its own knowledge: This is my GameObject, this is my SwordAttachPoint, this is my FireballSpawnPoint etc.