- Home /
Destroy box and show image
Hi,
I got the following situation. There's a box on my scene. If the player touches the box, the box will be deleted and an image has to appear on the screen.
So when I play the game and touch the box, the box will be destroyed but the image won't appear on the screen. However, if I take out the line to destroy the box, it does work. But I need the box removed too!
This is my script right now:
var show : boolean;
var GUI_Key : Texture2D;
function Start(){
show = false;
}
function OnTriggerEnter (other : Collider) {
//Get the item by touching it, then destroy the item
Destroy(gameObject);
//Put item image on screen
show = true;
}
function OnGUI () {
if(show){
GUI.DrawTexture(new Rect(10,10,40,39), GUI_Key);
}
}
Answer by Mander · Aug 28, 2012 at 02:44 PM
u destroy the object with the script. so it won't run the script after u destroy it. attach the gui scrpit on an emptygame object or on ur gui object but leave ur destrooy function on that cube. and before u destroy it change the variable on ur gui script.
function OnTriggerEnter (other : Collider) {
gameObject.Find("GuiObject").GetComponent("guiscript").show = true;
Destroy(gameObject);
}
Answer by Karsnen_2 · Aug 28, 2012 at 02:12 PM
Which gameobject are you destroying? Are you destroying the gameobject which has the GUI Texture as its own component?
I am destroying the gameobject that I touch, which is a simple box with the name Cube_$$anonymous$$ey. The script is attached to this gameobject. I think the GUI Texture is seperated.
Derrtyones. When you happen to destroy the gameobject, try to pause the game on the editor and check whether the required GUI Text component is present on the hierarchy panel.
Ok I noticed that when the box is destroyed, all that disappears is the "Cube_$$anonymous$$ey" object in the Hierarchy, which is the object that I touch. When I select the Cube_$$anonymous$$ey object and look at the script section in the inspector, I do see: "GUI_$$anonymous$$ey" and it shows the image behind it. By the way, I am quite new to this all.
So I am glad that you have found the mistake. Do feel bad that you are new, everyone have crossed your path and we are here to help you.
Well thank you but I am not sure how to solve this. How can I fix this?
Your answer
Follow this Question
Related Questions
Find Keyword in a Searched GameObject (JS) 2 Answers
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Is it possible to destroy an object at a specific position? 2 Answers
Co Routine 2 Answers
How to Display Map 1 Answer