0
Answered

How can I allow the player to pick up an instantiated object (and then destroy that object)?

Janus 2 months ago in Game Creator updated 2 months ago 7

Got an issue with this and I need help - I can't set it up the trigger On Collision Enter with the Player being the other collider because that requires the prefab I want to instantiate being moved into the Scene...and if I do that, the instantiated object becomes a clone and the 'destroy on finish' destroys the original item and not the clone.

I've even gone as far as to add a capsule collider trigger to the object I want to instantiate in case it was something to do with the object having a mesh collider. 

I need to be walked through this because I'm going to need this information for a lot of stuff :P

Unity version:
2019.1.7f1
Game Creator version:
0.9.6

Answer

Answer
Answered

Check out the latest Game Creator version 1.0.1 (available on the Asset Store tomorrow). The Instantiate Action comes with an optional parameter that allows you to store the instance created in a Local, Global or List Variable.


You can then use it to destroy the instance, rather than the prefab ;-)

Answer
Answered

Check out the latest Game Creator version 1.0.1 (available on the Asset Store tomorrow). The Instantiate Action comes with an optional parameter that allows you to store the instance created in a Local, Global or List Variable.


You can then use it to destroy the instance, rather than the prefab ;-)

Could you explain this to me with a bit more detail? At the moment I've got the standard instantiated object (in this instance, the soda can) and then I've got a local variable called Soda Variable stored inside it....but is it meant to be a game object, a boolean or what?


It just doesn't make sense to me at the moment.

Sure! So the thing is that you have a prefab (which let's remember, is not part of your scene, as it lives in your Project Panel). When you create an instance of a prefab using the Instantiate, you're basically creating a clone of that prefab and placing it in your scene.

So, up until now, you had an instance of this prefab in your scene, but no way to reference it. Now, with the new Instantiate Action, you can create a prefab instance in your scene and immediately store a reference to it in a Local Variable.

Let say that, for example, you want to create an instance of a prefab called MyObject, wait a couple of seconds, and later destroy it.

To do this, you'll need a Local Variable object where to store the reference of the instance of MyObject once it's instantiated. Here's a screenshot:

You can see at the bottom of the screenshot there's a Local Variable called "instance-reference" of type Game Object. In the above Actions, you create a new instance of the prefab and store this instance in this Local Variable. Then wait a couple of seconds, and finally destroy whatever object is inside the "instance-reference" variable (which is the instance of the prefab).

Sorry if this sounds verbose. Let me know if something's not clear :-)

Nope, it was clear as crystal and it works perfect now! Just one final question - how do I ensure that instantiated objects keep their rotation when being created? Because I've got the prefab set so that the cans are horizontal when I drag them into the scene, but when they are instantiated they are vertical and it doesn't look realistic. I would have thought the prefab determines the orientation but it doesn't seem to work in the case.

I believe that you can use the Transform Rotate Action. But despite this, the Instantiate action should also give you the default rotation. Could you post a screenshot of the Action, the result and how the prefab's transform component?

I've tried the Transform Rotate Action - it doesn't seem to work. 

https://media.giphy.com/media/J1j3oEeJehaxUBddaI/giphy.gif

+1

Never mind, I've figured it out - it works fine now :)