- Home /
Selecting One Piece, Moving to Selected Area
i have an array of gameobjects set up with a 'piece.js' script attached. here's what i'm having trouble with... i want to click one piece (with an OnMouseUp event) and have it sort of 'activate' to be moved. the problem is it activates everything. setting booleans inside that script didn't help, because it would just set true or false to everything the script is attached to.
i started to store the clicked object into an array like below ::
arr.Add(gameObject);
for (var go : GameObject in arr){ isSelected = true; }
this worked to an extent, but it then just adds objects to the array as i click them. is it possible to limit this array to 1 object? seems like something super basic but i'm at a loss.
Answer by · Feb 22, 2011 at 07:02 AM
Rather than "limiting the array to 1 object", just use a gameObject variable. i.e.
static var clickedObj : GameObject;
Alternatively - when you say "is it possible to limit this array to 1 object?", do you mean "is it possible to limit the array to 1 entry per object?" To do this, you'd iterate through the array, comparing whether or not the object already exists in the array before adding it.
Update as per comments:
You can store to the clickedObj
variable simply by assigning with '=
'. i.e.
clickedObj = gameObject;
is adding a gameobject to a gameobject possible? that'd probably be ideal, but i'd need to undo it as soon as the cycle completes. regarding the array, i meant limit .length to 1. sort of defeats the purpose of an array now that i think of it.
Yeah, I would suggest just using a GameObject var rather than a 1-length array. It's not really 'adding' to an array, you're just storing a reference to the GameObject. I've updated my answer with how to 'store the clicked object'.
hey dude, just wanted to say thanks for the help. it's really working out and fixed about a hundred problems. cheers.
Your answer
Follow this Question
Related Questions
selected object in array lost in translation 0 Answers
script optimization (choosing an object in game by click) 1 Answer
add waypoint to transform[] 2 Answers
Transport unknown amout of objects with GameObject array 0 Answers
View an array of the transform position/rotation of all game objects with a specified tag., 0 Answers