Disappear and Reappear

Hey. I am very new to this. I do not need to go very far with scripting for what I want to do. Having said that I do need to find a way to make game objects disappear and reappear. I have used destroyGameobject with good results on getting rid of the object, but then what? I looked into it and it seams my best approach is eaither to create prefabs so I can use an undo function. Use the instantiate function which I don’t understand much, but also requires prefabs. Or what seems most simple. enable.renderer=false. The only problem with this last option is that my FBX files I have imported don’t use a renderer to be displayed? I’m lost, any help for a beginner would be appreciated, thanks.

What is the actual intended use? Is this just to “go invisible”, is it like a shop where when they click on something you want it to appear so they can view it, is it for teleporting (eg, disappear here, reappear here).

There are different methods for different intended uses. Knowing the purpose would help.

Instantiate is actually pretty easy to do/use btw. Making a prefab is simple enough. You just need to determine the location of where you want to instantiate the prefab, and the rotation, and make the actual prefab. I’m sure myself (or others might beat me to it) can give code snippets as examples of how to instantiate or you can even use the search feature and find some.

In any event, it’ll be easier to adress the specifics of your problem when we have a clearer idea of what you want done.

Regards,
Rob

I’ve been experimenting with using “active.”

my line of code looks like this, sorta:

myGameObject.transform.active = false;

I haven’t gotten to the part of making it active again later… might be hard to find the object if it isn’t active, but I haven’t gotten there yet, and .active = false does make the thing disappear.

from the sounds of it, this is just what you need; Unity - Scripting API: Renderer.enabled

it basically turns off the mesh of the object (thats the thing that you can see) - as a result you can turn it on and off all you like with renderer.enabled = true or false :slight_smile:

So I tried using gameObject.active = false; and I believe this would work if it would deactivate all of the children as well. I tried a search and didn’t find a way to do this. Is there? Also why don’t my FBX models need a renderer, and why when I add a rigidbody to it does it disappear?.

I’m trying to deactive and reactive buildings in the map.

I use this to turn whole game objects on and off.

C# code:

gameObject.SetActiveRecursively(false);

naturally you could replace gameObject with something like

C# Code:

GameObject goMyObject = GameObject.Find("MyBuildingName");
goMyObject.SetActiveRecursively(false);
print("it is off now");
goMyObject.SetActiveRecursively(true);
print("it is on now");

Your fbx imports do have mesh renderers. That’s why you can see them. The part you see is the mesh that is being rendered (made visible by the computer). Something is screwey there. Maybe you have them set up as children of another object or something. Drag your fbx object into your scene, make a prefab, and drag and drop the object onto that prefab. To make changes, drag the prefab into the scene, make the changes, then drop it back onto the original prefab again to update it.

You could post a picture of a screenshot of Unity. that would help us solve your problem: to see how you have things set up.

The thing about adding a rigidbody is completely weird. Something is not right there.

Here is a screenshot[/img]

The thing about that basilica is that it isn’t a single object. It’s a group of objects, or so it seems. So the parent object (in the .fbx file) doesn’t have it’s own mesh, it’s sort of like an empty game object.

post again if you have any more questions.

I was thinking that. Is there a way to combine all of the meshes. The only thing I can think of is doing it in sketchup and for practical purposes, that isn’t much of an option for me, this project is going to encompass a lot of buildings… thousands possibly.

If the meshes children of a root object, you can switch rendering off for them all with something like this:-

function RecursiveShowHide(root: Transform, setting: boolean) {
  if (root.renderer) {
      child.renderer.enabled = setting;
  }

  for (child in root) {    
      RecursiveShowHide(child, setting);
  }
}

Ok I was able to get it all under one mesh. Works great!Thanks for the help everyone

hello, i need a little help with this
i am a noob in script, what i need in my game is… when the player enters in a room, I need that the wall in front of the camera gets invisible, then when the player go out, the wall gets visible again.
i guess that what i need is a activator in the floor or something like that I GUESS…
if you can please help would be very useful for my game
thank you…

hello…

i set gameObject.transform.render.enabled=false; and then i set that game object to layer=2 like this
gameObject.layer=2 (this will put it in the ignoreRaycast layer, which means that it will not react to any raycasting or collisions if you need that)

setting gameobjects.active=false is not a good idea to be honest, from many reasons. then you have to keep reference to the object
if you have to find it and make it active again.

also if you worry about frame rate you can use OnBecomeVisible for your objects

I think the script I need is complicated because it will become visible or invisible depending on whether I’m outside or inside a room
I’m sure I need something to turn it
im really noob :frowning: but thank you for help :slight_smile: