- Home /
Making a Game object appear and dissapear
I am attempting for an RTS Style, and I want this box to disappear, but for some reason it wont do anything.
var isSelected : boolean;
var mesh_on : boolean;
var currentUnit : GameObject;
var speed = 240.0;
isSelected = false;
mesh_on = false;
function Update () {
var Units = 1 << 8;
var Ground = 1 << 9;
var ray = camera.ScreenPointToRay(Input.mousePosition);
var hit_unit: RaycastHit;
if(Input.GetMouseButton(0)){
var tempUnit;
if(Physics.Raycast(ray, hit_unit, Mathf.Infinity, Units)){
print(hit_unit.transform.name);
if(hit_unit.collider.gameObject.tag=="Units"){
currentUnit = hit_unit.collider.gameObject;
tempUnit = currentUnit.MeshRenderer; isSelected = true; if(isSelected){ tempUnit.GetComponent(MeshRenderer).enabled = true; }else{ tempUnit.GetComponent(MeshRenderer).enabled = false; } }
}
else{
print("you hit nothing");
currentUnit = null;
		isSelected = false;
					if(isSelected){
						tempUnit.GetComponent(MeshRenderer).enabled
= true; }else{ tempUnit.GetComponent(MeshRenderer).enabled = false; } }
}
var pos = (Time.deltaTime * speed);
if(Input.GetKey("t")){
currentUnit.transform.Translate(0, 0, pos);
}
if(Input.GetKey("g")){
currentUnit.transform.Translate(0, 0, -pos);
}
if(Input.GetKey("h")){
currentUnit.transform.Translate(pos, 0, 0);
}
if(Input.GetKey("f")){
currentUnit.transform.Translate(-pos, 0, 0);
}
var ray_ground = camera.ScreenPointToRay(Input.mousePosition);
var hit_ground: RaycastHit;
}
Answer by deltamish · Nov 01, 2012 at 11:46 AM
LoL Even you try to make thing complex
Use .active = false;
I assumed he just wanted to make the object disappear. Using .active = false will also disable the collider and will not allow him to make the object reappear.
Answer by CuddleMonster · Nov 01, 2012 at 11:47 AM
to Destroy the object Do This: Destroy(gameObject);
to turn it invisible but it still function normally... Do This: GetComponent(MeshRenderer).enabled = false;
to turn the object off, but not destroy it (you can turn it back on again later) Do This: gameObject.active = false;
If I do that then it tries to turn off the mesh renderer of the camera
obviously if the script is on the camera and you write just GetComponent($$anonymous$$eshRenderer).enabled = false; it will not work. you need to give it an object reference.
SelectedUnit.GetComponent($$anonymous$$eshRenderer).enabled = false;
I'd help you figure out how to work it exactly but since you don't comment your work its a little difficult.
Answer by attar033 · Nov 01, 2012 at 11:51 AM
In your code, every unity has to do a Raycast check to see if it is selected. This is abit of an overkill. I would suggest that you have a different script that manages user input. This would look something like this (in c#)
public class InputManager : Monobehaviour
{
public LayerMask unitsMask;
void Update()
{
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
RayCastHit hit;
if(Input.GetMouseButtonDown(0) && Physics.Raycast(ray, out hit, Mathf.Infinity,unitsMask ) )
{
UnitController unit = hit.transform.GetComponent<UnitController>();
if(unit!=null) //this check is not really needed as the units will all have the same unique layer
{
//I just did a simple selection toggle. you might need a more complex user selection behaviour
unit. isSelected = !unit.isSelected;
unit.transform.gameObject.renderer.enabled = unit.isSelected
}
}
}
}
I apologise if I wrote this in C# but I think you get the general idea.
Your answer
Follow this Question
Related Questions
How to basically use GUI.Toggle? 2 Answers
Instantiated GameObject Not Rendering 1 Answer
enable and disable boxcollider (whats wrong with my script?) 2 Answers
How can I control GameObject visibility? 2 Answers
Game Objects enable problems 0 Answers