- Home /
Toggling active between objects using a boolean
Hey guys,
I'm having a little difficulty with a unit selection system I'm working on. I have a script (see below) attached to two objects, and what I'd like is for the player to click on one, and it become 'activeUnit'. As a visual aid, I've made them change colours depending on if they are active or inactive.
My problem is this : I can select them fine, but for the life of me I can't devise a way to only select one at a time. When you click on object B, I need object A to be unselected, and visa versa.
If anybody could point me in the right direction I'd be very grateful.
private var activeUnit : boolean = false;
function OnMouseDown () { var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition); var select : RaycastHit;
if (Physics.Raycast (ray, select, 100.0))
{
//print ("selected " +select.collider.gameObject.name);
activeUnit = true;
} else {activeUnit = false;}
}
function Update () {
if (activeUnit==true) {
renderer.material.color = Color.green;
} else if (activeUnit==false){renderer.material.color = Color.red;}
}
Answer by tool55 · Feb 10, 2011 at 02:35 PM
var myCubes : GameObject[];
function Start() { for (var thisCube in myCubes) { thisCube.renderer.material.color = Color.red; } }
function Update () { if (Input.GetButtonDown ("Fire1")) { var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition); var select : RaycastHit;
if (Physics.Raycast (ray, select, 100.0))
{
for (var thisCube in myCubes)
{
if( select.collider.gameObject ==thisCube)
{
thisCube.renderer.material.color = Color.green;
}
else
{
thisCube.renderer.material.color = Color.red;
}
} } } }
That almost works, the only issue is it affects every unit with this script attached. It also toggles them active/inactive no matter what I click.
You would have to use your "select" variable to find which collider has been hit by the raycast and then check the tag or name or other parameter in an if statement to have it respond to that specific game object. Also, I wouldn't put this script on multiple game objects. Seems like it would be better to create variables for the game objects, then drag those game objects to the variable in the inspector.
Actually, I think I misinterpreted what you want. I'll rework the script when I have bit of time later. I thought you wanted to toggle between two objects on each click, but you want to make them selectable. $$anonymous$$y bad.
O$$anonymous$$. I modified the script above so that only one object is selected at a time. I made it an array so that you can have as many objects as you like. The one that's clicked will turn green. All others will turn red.
What does this need (scene wise) to work? I've just got the same script applied to two spheres at the moment. I'm beginning to think I may need to learn how to build a class for 'units', so that I can target them externally, without needing to name each instance (and avoid limiting the amount of units). Sound right?
Answer by Jessy · Feb 10, 2011 at 01:47 PM
First off, OnMouseDown already uses raycasting, so don't bother with that, unless you really need that distance of 100. I don't know what your script is called, or what else you have going on. I might do it this way, in JavaScript:
var otherUnit : Unit; @HideInInspector var _active : boolean = false;
function OnMouseDown () { _active = true; renderer.material.color = Color.green; otherUnit._active = false; otherUnit.renderer.material.color = Color.red; }
However, I really don't feel that JS is cut off for this kind of stuff. If you use C#, there is an event system, which can improve the code in many ways. If you have interest in this, and don't understand it, feel free to ask any questions about it you want.
using UnityEngine;
public class Unit : MonoBehaviour {
// "new" allows you to override the deprecated Component.active, // but is active even necessary? new bool active; new Renderer renderer;
delegate void EventHandler (); event EventHandler MouseDown; static event EventHandler SwitchActive;
void Awake () { renderer = GetComponent<Renderer>(); MouseDown += Activate; }
void OnMouseDown () {if (MouseDown != null) MouseDown();}
void Activate () { if (SwitchActive != null) SwitchActive(); active = true; renderer.material.color = Color.green; MouseDown -= Activate; SwitchActive += Deactivate; }
void Deactivate () { active = false; renderer.material.color = Color.red; SwitchActive -= Deactivate; MouseDown += Activate; }
}
Your answer
Follow this Question
Related Questions
Help - Selection System 1 Answer
Math/Programming Question 4 Answers
area selection (mouse drag) on overhead game 1 Answer
Advance when the sound is done playing 1 Answer
Raycast - screen position to world position not precise 0 Answers