Item show w/ python script

I’ve been working on a script which would show an item if a button is pressed and when that button is pressed again then the item would disappear. The only two hitches are that this needs to be scripted and it can only use .addObject() and .endObject(). I’ve made the script so it should logically work but blender always crashed when the item is being ended. Also the button needs to be able to add multiple objects in a row and they could all be the same objects that it adds.

I’ve used empties as reference points for where the added object goes. There are also four buttons that when clicked will change the empties visible property to 1 or 0. when the empties visible value changes the it runs a script that checks a main controller(which works flawlessly) that tells it which button has been clicked. Then the script uses the info from the main controller and when add the corresponding object. This object is found by looking at its other properties(one for each of the buttons) and adding the object with the same name as the property but also ends the other objects(even if there isn’t any) as a precaution so I don’t have any overlap of objects. I’m using blender 2.58.

is there any better way of doing this?

Thanks, Bearmey

P.S. here’s my code for the empty:

import bge

cont = bge.logic.getCurrentController()
scene = bge.logic.getCurrentScene()
Vis21 = cont.sensors[“Vis2=1”]#the sensor that activates this code when this property is changed
Button1 = cont.owner[“Button1”]
Button2 = cont.owner[“Button2”]
Button3 = cont.owner[“Button3”]
Button4 = cont.owner[“Button4”]
MainController = scene.objects[‘MainController’]

#If statement which checks the sensor if it is positive
#if so then will add the right item
if(MainController[“Visible”] == 1):

    • scene.addObject(Button1, cont.owner)
    • scene.objects[Button2].endObject()
    • scene.objects[Button3].endObject()
    • scene.objects[Button4].endObject()
    • print(“Button1”)
      elif(MainController[“Visible”] == 2):
    • scene.objects[Button1].endObject()
    • scene.addObject(Button2, cont.owner)
    • scene.objects[Button3].endObject()
    • scene.objects[Button3].endObject()
    • print(“Butto2”)
      elif(MainController[“Visible”] == 3):
    • scene.objects[Button1].endObject()
    • scene.objects[Button2].endObject()
    • scene.addObject(Button3, cont.owner)
    • scene.objects[Button4].endObject()
    • print(“Button3”)
      elif(MainController[“Visible”] == 4):
    • scene.objects[Button1].endObject()
    • scene.objects[Button2].endObject()
    • scene.objects[Button3].endObject()
    • scene.addObject(Button4, cont.owner)
    • print(“Button4”)
      else:
    • scene.objects[Button1].endObject()
    • scene.objects[Button2].endObject()
    • scene.objects[Button3].endObject()
    • scene.objects[Button4].endObject()