Tool script affecting all instances

Godot Version

v4.2.2.stable.mono.official [15073afe3]

Question

I made this tool script to more easily change the size of a box:

@tool 
class_name SizeTool
extends Node3D

@export var size : Vector3
var shape3D : BoxShape3D
var mesh3D : BoxMesh

@export var apply : bool :
	set(value):
		_apply()
		apply = false

func _ready():
	shape3D = get_node("CollisionShape3D").shape
	var mesh_instance = get_node_or_null("MeshInstance3D")
	if mesh_instance:
		mesh3D = mesh_instance.mesh 
	else: mesh3D = null

func _physics_process(_delta):
	if !Engine.is_editor_hint():
		return
	
	if size == Vector3.ZERO:
		return
	
	#print(mesh_instance)
	
	shape3D.size = size
	if mesh3D != null : 
		mesh3D.size = size
	return

func _apply():
	shape3D = get_node("CollisionShape3D").shape
	var mesh_instance = get_node_or_null("MeshInstance3D")
	if mesh_instance : mesh3D = get_node_or_null("MeshInstance3D").mesh
	
	if size == Vector3.ZERO:
		print("can't set to 0")
		return
	
	shape3D.size = size
	if mesh_instance : mesh3D.size = size
	return

I have a “block” scene wich is just a staticbody3D, with a collisionshape3D and a meshInstance3D as children. The script works successfully to change the size of the block, but if I drag multiple blocks into a scene, they take the same size. This happens even if I make the blocks local. So how can I change the size on singular instances?

You have to make the mesh and the shape local, not the box itself

Shape3Ds are Resources. Resources are shared by default. You’ll need to make them unique:

  • Right click over it in the inspector and click on Make Unique
  • Enable Local to Scene in the Resource. This will make unique the resource when instantiating the scene (if the resource is shared between nodes in the same scene the copies won’t be made unique)
  • Calling Resource.duplicate() in code. For example in your _ready() function.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.