- Home /
How to delete an instantiated object
So it seems this is a problem people have encountered before, but none of the code looks like what my Instructor has shown us, and I'm having trouble deleting my object.
What happens is, you push a button and it instantiates a colored cube to the player. What I'm trying to make happen, is when the player has a cube and clicks on a matching cube on a door, it disappears and the cube on the door changes color to indicate it's 'unlocked'.
So the first bit is how we get the key when we push the button.
var KeyPreFab: GameObject;
var ThePlayer: GameObject;
var TheKey: GameObject;
function Start () {
}
function Update () {
}
function OnMouseUpAsButton () {
Debug.Log("Button has been pushed.");
renderer.material.color = Color.green;
//turn yellow key checkbox on
if (ThePlayer.GetComponent(PlayerInventory).YellowKey == false) {
TheKey = Instantiate (KeyPreFab, ThePlayer.transform.position, Quaternion.identity);
TheKey.transform.parent = ThePlayer.transform;
TheKey.transform.localPosition.y += 0.5;
TheKey.transform.localPosition.z += 0.5;
}
And I imagine that in my script for the door's 'keyhole' it will start off something like
if (ThePlayer.GetComponent(PlayerInventory).YellowKey == true) {
}
but I honestly don't know how to approach this after that. I've tried rendering the key false, I've tried destroying it... The 'keyhole' doesn't even change color for me, and the code so far is exactly the same code I have above (but with a different message and color render).
I hope I've given enough information to be useful. I thought this would be an easy thing to do for my assignment, but I guess not :P.
Hi Skittlesloli
Have you tried throwing in some Debug.Log("Button pressed!") calls just to make sure your keyhole is working?
Yes, I have the "button pressed" changed to "you have used the key". I have also set the keyhole to turn black, and that's not happening either. Edit: Thank you Bored$$anonymous$$ormon, for the edit. I wasn't sure how to fix that.
Perhaps you could post some of that code as well? I think that's the most important thing to see for us to help you.
Sure :P
var $$anonymous$$eyPreFab: GameObject;
var ThePlayer: GameObject;
var The$$anonymous$$ey: GameObject;
function Start () {
}
function Update () {
}
function On$$anonymous$$ouseUpAsButton () {
Debug.Log("You have used the key.");
renderer.material.color = Color.black;
//turn yellow key checkbox on
if (ThePlayer.GetComponent(PlayerInventory).Yellow$$anonymous$$ey == true) {
//this is where I get lost
}
Your answer
Follow this Question
Related Questions
How to destroy instantiated objects. 1 Answer
Cannot destroy child object in prefab- Error 1 Answer
Check if child exists and instantiate as child 1 Answer
How to spawn a 'boss' after all enemies defeated and then kill that 'boss'? 1 Answer
How to spawn a new prefab when the last one was destroyed?? 2 Answers