Niantic Studio: How do you make an object in the world disappear after you collect a collectible?

I followed the Niantic Studio Game Mechanics Basics Workshop and made a collectible coin. Now I’m trying to figure out how to make another object in the world disappear after collecting the coin. Like if you have a key object that you collect to open a door somewhere else.

I know it has something to do with “world.deleteEntity(eid)” but I don’t know where to put the code, and I’m not sure how to make the code target the object I want to disappear. Where do I put the object’s name in this code? And then where do I put the code so it will make the object disappear after you collect the coin?

Hi!

In Studio, everything in your experience is either an entity or a component.

An entity represents something that exists in the world of your experience, but it doesn’t always have a visible representation.

A component is attached to an entity, and entities can have any number of components. The most common components you’ll see are Position and Rotation, which define the entity’s location and orientation.

To remove an entity from your scene, you can use world.deleteEntity(eid). This function takes a single argument, which is the entity’s ID (eid). So the first step is identifying the entity in your scene and passing its eid to the function.

In your case you’d want the logic to look like this:

  1. Character makes a physics collision with an object.
  2. Check if the object has the ‘Coin’ component.
  3. If it does, delete the object the player collided with.
  4. Do some logic to increment the score or play a sound, etc.

This logic would most likely live as a custom component on your character, along with a collectable custom component that could have it’s own information like worth.

Here’s a helpful page on your docs that goes into specific details:

In the example you gave, the code is just deleting the collectible. I want to delete the collectible and also delete another object in the world. I’ve got the collectible working, I just don’t know how to delete the other object. Here’s how I’ve been trying to get it to work:

  1. Character makes a physics collision with an object.
  2. Check if the object has the ‘Coin’ component.
  3. If it does, delete the object the player collided with.
  4. Do some logic to increment the score or play a sound, etc.
  5. Delete another object in the world with world.deleteEntity(Plug in object’s Entity ID?)

I just don’t know where an object’s Entity ID is. I thought this code was to set an object’s Entity ID, but I’m starting to think this isn’t it.

Screenshot 2024-09-27 150032

Coin in this case is the component you’re registering, not the entity. If you want to get the entity id you either need to get it from some event like a physics touch or create a new entity programmatically using const test = world.createEntity() where test would be your entity id.

If you’re trying to get the entity that the company is currently attached to you can do component.eid.

Hi Focus, I think if the objects are reusable, they shouldn’t be deleted and recreated because it could affect performance. The best thing is to hide them by setting the scale value for them.
For example:

world.setScale(eid, 0.00001, 0.00001, 0.00001)

And be sure to set the logic to false when you hide them.
For example, the logic of that entity should only run when active === true

if(active) {
  *Put your logic here!*
}"
1 Like