- Home /
Hide object when changing instantiation
I have a script which allows me to instantiate different objects with a "ghost object", which is the same object that's being instantiated, so I can rotate it real-time. Now, when I change between the objects the ghost object changes as well, which is good. Although now I have a problem. The ghost object is placing on the terrain when I instantiate, which seems logical. Is there any way I can "hide" the ghost object and when I click on a button I can show it again? What's the best way to do this?
Thanks
Answer by Tomer-Barkan · Oct 13, 2013 at 01:06 PM
What is a ghost object? Do you mean a prefab? Sharing your code might help.
If you want to instantiate an object and make it hidden from view, use this code:
GameObject newObj = (GameObject)GameObject.Instantiate(prefab);
newObj.renderer.enabled = false;
The second command will turn the renderer off, hence the new object will not be visible. Turn it back on if you want to make it visible again.
The ghost object is the same prefab that gets placed down because I, as I said, need to rotate it in real-time. So that looks promising, but how can I only hide the object that follows the camera?
if (chosenObject == 1) {
Object1.transform.position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
if (Input.GetButton("RotateInst")){
Debug.Log ("RotateInst cliked");
Object1.transform.Rotate(Time.deltaTime, 1, 0);
}
}
if (chosenObject == 2) {
Object2.transform.position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
if (Input.GetButton("RotateInst")){
Debug.Log ("RotateInst cliked");
Object2.transform.Rotate(Time.deltaTime, 1, 0);
}
}
if (chosenObject == 3) {
Object3.transform.position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
if (Input.GetButton("RotateInst")){
Debug.Log ("RotateInst cliked");
Object3.transform.Rotate(Time.deltaTime, 1, 0);
}
}
I don't know what "the object that follows the camera" is, but you simply set it's renderer to disabled:
objectThatFollowsTheCamera.renderer.enabled = false;
Then when you instantiate a new object, you set it to true again:
GameObject newObj = (GameObject)GameObject.Instantiate(gameObjThatFollowsTheCamera);
newObj.renderer.enabled = true;