How do you get object scenes to truly be separate when instantiating and coding them?

Godot Version

4.6.1

Question

I’ve got a scene with some instantiated rigidbody cubes with a script attached to the cube in its scene. I’ve made all the parts unique and local to scene which did help but they still aren’t truly separate. For example, when I delete one of them all of them lose their physics. And when calculating their rotation if one cube moves it changes the rotation for every cube. How would I go about fixing this and getting all the instances to be separate, and is there a way to do that while having the cubes use the same script or do you have to do some extra shenanigans to make it work?

That shouldn’t happen, unless you’re executing these commands directly within the script - then all Nodes that have that script attached will be affected by it.

We can’t really judge anything without seeing your script and the relevant scene structure.

6 Likes

I am executing the commands within the script attached to the cube. I have a scene set up with some cubes instantiated and they’re all under a Node3D parent like this:


I’m just testing stuff right now and in the script attached directly to the cube scene I’m trying to find the rotation of the selected cube and apply a force to it upon pressing a button. Each cube can be clicked on and when clicking on one it’ll be marked as selected. The code looks like this:


Whenever the force is applied on the selected cube its rotation will effect every other selected cube and cause the force to applied incorrectly. This also happened when trying to simply delete the selected cube earlier where deleting one would keep the other cubes present but their physics would be removed.

How should I go about scripting this stuff so that each cube instance behaves the same but doesn’t effect each other?
Thanks in advance!

I think the brick_node variable is the problem. It’s likely attached to the general scene.

In order to make an unique instance of your brick, you’ll need to one of 2 things:

Either store each separate brick as its own variable (If you aren’t planning to spawn any new bricks at runtime):

@onready var Brick_1 : Node = $YourBrickNode1
@onready var Brick_2 : Node = $YourBrickNode2
@onready var Brick_3 : Node = $YourBrickNode3
@onready var Brick_4 : Node = $YourBrickNode4

# Etc

Or, and likely the better option, instantiate and spawn the bricks at runtime. Then store them in a brick array:

@onready var Brick_Scene : PackedScene = preload("your_brick_scene")

var Brick_Array : Array[Node] = []

func spawn_bricks() -> void:
        
   var New_Brick : Node = Brick_Scene.instantiate()

   add_child(New_Brick)

   Brick_Array.append(New_Brick)

This will add your newly spawned brick to an array. An array is essentially a list of data.

To call upon a specific instance in an array, do the following:

Brick_Array.get_node(YourBrickNodeName)

You can read more about arrays here in the documentation:

Dictionaries also work for this.


Side note, you can format code on the Godot Forum using this button on the toolbar:

image

Use that instead of screenshots to make it easy to read, copy, and paste code.

type or paste code here
2 Likes

I see, thanks a lot! I’ll make sure to look into Arrays and try to get an understanding of them, especially since they seem to be very important

And thanks for the reminder! I forgot about being able to format code so that’s my bad, I’ll make sure to do that from now on

1 Like

No worries. Every new member makes that formatting mistake.

Arrays are also super interesting and fun to work with when you get the hang of them

I’m glad I could help.

1 Like