A way to check for an object on a scene via python?

Is there a way how to check for an object name if it exists on a scene?

You can try:

import bge

cont = bge.logic.getCurrentController()
own = cont.owner

# Loop over scene objects
for obj in own.scene.objects:
    
    # Check if obj in current iteration is the one we're searching
    if obj.name == "TheObjYoureLookingFor":
        
        # Do something else with your object
        print("This object exists:", obj)
        break # Stop loop here once we found the object

But I seriously don’t recommend doing this frequently since it loops through all objects in the scene, and running this each frame on a scene with lots of objects will surely slow down your game. If you need to do something frequently on this object try searching for it, storing a reference to it, and run the code based on the stored reference. In this case, an update of the code above:

import bge

cont = bge.logic.getCurrentController()
own = cont.owner

# Check if there's not a property named ObjectReference in owner
if not "ObjectReference" in own:
	
	# Loop over scene objects
    for obj in own.scene.objects:
		
		# Check if obj in current iteration is the one we're looking for
        if obj.name == "TheObjYoureLookingFor":
			
			# Store obj reference in owner as a game property
            own["ObjectReference"] = obj
            break # Stop loop here once we found the object
            
# If object reference was already stored in owner            
else:
	obj = own["ObjectReference"]
	
	# Do something else with your object
	print("Object reference:", obj)
1 Like

And @Saranoske, in case you weren’t aware, you can easily get/set other object attributes/properties in other scenes in Python.