- Home /
Make Mesh & Text disappear
Hi, so I have my game project where the player is asked questions, responds by going through doors, and it instantiates a new room prefab on the other side of the door for the player to go through, however I'm having an issue where the old room and text that was created for it still linger and cause some confusion for the player and obscure the part of the new room and new question text.
I'm not exactly sure what to do in this case. I'm not sure how to tell the mesh render for the previous room to turn off, delete, or become transparent to some degree when the player passes through the door or triggers a condition. I need some solution to at the very least get rid of the text from the previous room. The text, which is generated by reading a text file, is being generated through a script that goes like this.
if(gameObj.name == "Body")
{
objText.Add( new GameObject("TextObject") );
TextMesh textMesh = objText.Last().AddComponent<TextMesh>();
Font ArialFont = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf");
textMesh.font = ArialFont;
//textMesh.text = "Hello World!";
textMesh.text = Util.WordWrap ( "A" + (doorAnswer+1) + ") " + content.answers[doorAnswer], 10 );
textMesh.renderer.material = ArialFont.material;
//Answers
GameObject door = gameObj;
textMesh.transform.rotation = door.transform.rotation;
textMesh.transform.position = new Vector3(door.transform.position.x - 6, door.transform.position.y + 10, door.transform.position.z); //- cube.renderer.bounds.size.x / 2
textMesh.transform.Rotate(Vector3.up, -90, Space.Self);
//0th is left door, 1th center, 2nd right
if (doOnceOnly == 1){
//put question above center door
//Question
objText.Add( new GameObject("TextObject") );
TextMesh textMesh2 = objText.Last().AddComponent<TextMesh>();
textMesh2.font = ArialFont;
textMesh2.text = Util.WordWrap ( "Q" + content.qNum + ") " + content.question, 20 );
textMesh2.renderer.material = ArialFont.material;
textMesh2.transform.rotation = door.transform.rotation;
textMesh2.transform.position = new Vector3(door.transform.position.x - 8, door.transform.position.y + 20, door.transform.position.z); //- cube.renderer.bounds.size.x / 2
textMesh2.transform.Rotate(Vector3.up, -90, Space.Self);
doOnceOnly++;
}
else {
doOnceOnly++;
}
doorAnswer++;
Also, after the first room, for some reason, the Question text is always off to the side instead of centred and I can't seem to figure out why. Any ideas on what exactly I should do in this situation?
Answer by Berenger · Nov 05, 2013 at 11:19 AM
Once you get to a new room, you'll have to hide the stuff from the previous one by using SetActive, renderer.enabled or a transparent material.
For that, you'll need the reference of the objects from the previous room. Is suggest a to parent them to a single gameObject.
Answer by MattTheProgrammer · Nov 05, 2013 at 11:41 AM
From what I see you want to destroy a gameObject that the script is not attached to.
Make the text dissapear:
GameObject.Find("Name").GetComponent(MeshRenderer).enabled = false;
As for the question always off to the side, Maybe you could keep messing around with the spawn co-ordinates until you find something thats good.
Also, you should name you new text something different to the other ones
Any clue on how to do that? Also, I put in your fix and Unity keeps giving me errors. First I put it alone in my code and that didn't work, than I assigned it to GameObject variables and that doesn't work either. Exactly how should that be implemented?
EDIT: I was able to use the Destroy command to delete the text. HOWEVER, it won't stop deleting the text. As soon as I open a door to trigger the text's destruction, all text is destroyed including the text in the next room, which is supposed to still be present.