- Home /
How to change the tag of one object, when another object is selected.
Hello,
I have a scene with several stationary capsules. The capsules are parented to either a first sphere or a second sphere based on their position. In my scene I want to be able to turn the sphere (and the children) left or right with a GUI button when one of the capusles is selected. Everything works fine when a first object is selected and I try to rotate the spheres. My problem is, when a second, third, etc capsule is selected. The GUI buttons begin to overlap, and eventually the tags for each object and GUI buttons get so intertwined and messed up I can't continue.
I've tried the destroy GUI function which successfully got rid of the GUI buttons as well as changed the object tag. But... if I want to make 5 successive turns, I have to select the object, hit the GUI button, select the object, hit the GUI button, and so on and so forth.
I'm hoping someone can provide some advice or an alternate route to explore. Also, I've attached my code below for a little more clarity.
#pragma strict
var capsule : Transform;
var clicked : boolean = false;
var hit : RaycastHit;
function Update() {
if(Input.GetMouseButtonDown(0) &&
collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit,
Mathf.Infinity)) {
clicked = !clicked;
if(tag == "Select") tag = "Unselect";
else tag = "Select";
Debug.Log("clicked" + (clicked? "" : " off"));
}
}
function OnGUI()
{
if (tag == "Select"){
var someText = "Turn Left";
var otherText = "Turn Right";
if (GUI.Button(new Rect(20, 50, 100, 20), someText)){
capsule.parent.transform.Rotate (0, 0, 45, Space.World);
}
else if (GUI.Button(new Rect(850, 50, 100, 20), otherText)){
capsule.parent.transform.Rotate (0, 0, -45, Space.World);
}
}
else if (tag == "Unselect"){
}
}
After some thinking, I might try and define the GUI buttons to always be on the screen and then set "var capsule : Transform;" to be equal to the transform of the object that was struck by the raycast. That way I take the switching of tags out of the process all together.
Your answer
Follow this Question
Related Questions
Disabling Raycast with GUI 0 Answers
Problem with GUI Buttons with WorldSpace Canvas 2 Answers
Is there a generalized version of GUIUtility.hotControl? 0 Answers
How to connect RayCast and GUI Text? 1 Answer