playMaker

Author Topic: Error destroying an object  (Read 10129 times)

Rabagast

  • Hero Member
  • *****
  • Posts: 683
    • Homepage
Error destroying an object
« on: August 20, 2013, 04:58:13 PM »
Hi!
I have set up a trigger on the bullet, so when the bullet hit an object, the object will be destroyed. But I get this error:

"Destroying assets is not permitted to avoid data loss."

What does that mean? I also tried to set it to invisible, but that doesn't seem to work.

Thanks!
Check out our homepage. http://www.walsberg.no
Or my personal game blog for news about my games. http://retro-tetro.walsberg.no

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Error destroying an object
« Reply #1 on: August 20, 2013, 05:09:50 PM »
It sounds like the Destroy Object action is pointing at a prefab (asset) instead of a scene object (e.g., an instance of the prefab in the scene).

You should store the object the bullet collided with in a GameObject variable and then use that in Destroy Object.

Alternatively, it can be a good design choice to let the object being hit decide how to react. E.g., the object has an FSM that reacts to a HIT event that triggers a "got hit" state with a Destroy Self action. Then the bullet can just send a HIT event to the object that it collides with.

If you need to send more info, like damage amount, you can use Set Event Data on the bullet and Get Event Info on the hit object.

jess84

  • Hero Member
  • *****
  • Posts: 515
Re: Error destroying an object
« Reply #2 on: August 20, 2013, 05:38:30 PM »
Out of interest, how to you do that solution if you are instantiating an object from a prefab? (e.g. you don't start with that object in your scene).


Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Error destroying an object
« Reply #3 on: August 20, 2013, 06:32:56 PM »
Do you mean the Destroy Self part? Destroy Self will refer to the prefab instance at runtime, not the prefab.

jess84

  • Hero Member
  • *****
  • Posts: 515
Re: Error destroying an object
« Reply #4 on: August 20, 2013, 09:07:33 PM »
Yes. That message is the same as what I was getting when I selected my prefab as the object to destroy. I couldn't work out how to make it relate to the prefab instance, rather than the prefab itself. (the same problem the original poster was having)

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Error destroying an object
« Reply #5 on: August 20, 2013, 11:22:43 PM »
You can use Get Collision Info and store the Game Object Hit in a GameObject variable. Then use that variable in Destroy Object.

Rabagast

  • Hero Member
  • *****
  • Posts: 683
    • Homepage
Re: Error destroying an object
« Reply #6 on: August 21, 2013, 12:57:47 PM »
Hi!
Thanks for the answers!

Alex Chouls:
I tried to make a GameObject variable and put the Prefab there, but it doesn't seem to work. I tried the same with a GameObject from the Scene, and that works fine. And I can't use a GameObject from the scene in a create object from a PrefabObject, and vice versa.

Here is a more detailed description of what I want to do:

I have a PrefabBullet where I have a trigger event.
When the bullet hits a PrefabAsteroid, the asteroid will explode. The problem is, that I can't destroy a Prefab.
Check out our homepage. http://www.walsberg.no
Or my personal game blog for news about my games. http://retro-tetro.walsberg.no

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Error destroying an object
« Reply #7 on: August 21, 2013, 02:40:28 PM »
When you point to a Prefab you're pointing directly to an asset in the project, not an instance in the scene. Storing the prefab in a GameObject variable is the same as pointing to the prefab. You have to find a scene object to point to.

Create Object, for example, has a field: Store Object that you can use to store the created instance in a GameObject variable. You can then use that to point to the created object.

Or after a collision you can get the hit Game Object using Get Collision Info.

The best approach in many cases is to have the bullet send a "Hit" event to the hit object. The hit object has an FSM that responds to a "Hit" event.

Bullet FSM:
When a bullet collides with an object, it uses Get Collision Info to get the colliding object. It then uses Send Event to send the "Hit" event to that object. Then Destroy Self to delete itself.

Asteroid FSM:
Has an Idle state with a Hit event triggering a transition to an Explode state. When the asteroid gets a Hit event from the bullet, it transitions to Explode. Explode spawns some particles, plays a sound effect, then calls Destroy Self.

With this setup different objects can each respond to the "Hit" event in its own way. It's also a lot more flexible, letting you quickly explore gameplay ideas. What if debris from an explosion can send a "Hit" event too? You get cascading explosions! Or maybe the player ship could get a shield pickup that sends "Hit" events, destroying any asteroid it plows through...

Does that make sense?

Rabagast

  • Hero Member
  • *****
  • Posts: 683
    • Homepage
Re: Error destroying an object
« Reply #8 on: August 21, 2013, 03:40:43 PM »
Hi!
This is what I'm doing now!
I set up a FSM on the PrefabBullet.
On state 1 I have
-Get Collision Info where I have a GameObject variable I call "Asteroid"
In Game Object Hit, I use this variable

Then I have a Send Event. And in Event target, what do I need to have there?
I set it to Game Object. I don't know if it's correct. And I made an event called HIT.
Does this event need to be GLOBAL?
In state 2, I have Destroy Self!

Do I something correct here? Or is it all wrong? :)
But something must be wrong, because the bullet will not destroy it self.

Another thing is that I can't use a scene object as a create object in a PrefabObject.
If I set up a Create Object on a PrefabObject, I need to use another PrefabObject.
If I set up a Create Object on a Scene Object, I need to use another Scene Object.
Check out our homepage. http://www.walsberg.no
Or my personal game blog for news about my games. http://retro-tetro.walsberg.no

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Error destroying an object
« Reply #9 on: August 21, 2013, 04:34:27 PM »
Yes, send the event to the hit object, and the event should be marked Global.

I've attached screenshots that should help...

Of course the asteroid can do a lot more than Destroy Self when it Explodes...

Rabagast

  • Hero Member
  • *****
  • Posts: 683
    • Homepage
Re: Error destroying an object
« Reply #10 on: August 21, 2013, 04:59:43 PM »
Thanks!
I will check it out tomorrow! :)

I have just two question:
In state one, do you have "On Trigger Enter" in the bullet FSM?

And in the idle state in Asteroid FSM, what do you have there?
Check out our homepage. http://www.walsberg.no
Or my personal game blog for news about my games. http://retro-tetro.walsberg.no

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Error destroying an object
« Reply #11 on: August 21, 2013, 05:09:11 PM »
The Idle state is empty, but in a real game it could control whatever ambient behavior the object has. E.g., rotate slowly...

The Bullet FSM Fired state just has an Add Force to move the bullet. COLLISION ENTER is automatically sent when the GameObject the FSM is on collides with another object. The bullet has a rigid body and the asteroid has a sphere collider, so when the bullet touches the asteroid it gets a COLLISION ENTER event.

Rabagast

  • Hero Member
  • *****
  • Posts: 683
    • Homepage
Re: Error destroying an object
« Reply #12 on: August 22, 2013, 03:49:55 PM »
Now I have tried it, without any succeed!

I can fire a bullet, but when the bullet hits the asteroid, nothing happened.
In the asteroid FSM, I have the Idle State, but this state is not going to the next state "Explode" when the bullet hits the asteroid.

Are you sure it's working? Or maybe I'm doing something wrong. I think more the last :)
Check out our homepage. http://www.walsberg.no
Or my personal game blog for news about my games. http://retro-tetro.walsberg.no

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Error destroying an object
« Reply #13 on: August 22, 2013, 04:54:53 PM »
Yeah, I built a little test scene to take the screenshots and it worked fine...

I've attached a unitypackage...

Rabagast

  • Hero Member
  • *****
  • Posts: 683
    • Homepage
Re: Error destroying an object
« Reply #14 on: August 23, 2013, 12:49:52 PM »
Thank you! Now it's working! :)
The bullet and the asteroid had the "Is Trigger" checked, so I did need to uncheck it. And also "RigidBody" on the asteroid. But the asteroid can't destroy itself, because I need to respawn it. I just make the asteroid invisible, and then make it visible again after 3 sec. It works fine. And position it.

I was going to ask for this in a new post, but I do it here :)
How can I make a random position for the asteroid?
Check out our homepage. http://www.walsberg.no
Or my personal game blog for news about my games. http://retro-tetro.walsberg.no