- Home /
Private Array in custom Class behaving Public
Hey Unity peeps,
I have a custom class that can have any number of instances generated at runtime. I am building a very literal "snapping" matrix (multi-dimensional, jagged array) specific to each instance of the class. The issue is that all instances of the class seem to be manipulating the same array instead of their own instance of said "private" array (snapToLocation).
// da class
class LineObject {
private var name : String;
private var id : int;
private var startPoint : Vector2;
private var endPoint : Vector2;
private var lineLength : float;
private var maxLength : int = 12;
private var snapToLocation = Array(maxLength);
public function LineObject () {
// prep item "snapping" matrix
for (var i : int = 0; i < maxLength; i++) {
snapToLocation[i] = new Array(3);
snapToLocation[i][0] = "available";
snapToLocation[i][1] = "available";
snapToLocation[i][2] = "available";
}
}
// getters, setters, etc...
}
// and elsewhere ... da instantiation
function CreateLineObject (line, id) : LineObject {
var vectorPoints : Vector2[] = line.points2;
var startPoint : Vector2 = vectorPoints[0];
var endPoint : Vector2 = vectorPoints[1];
var lineLength : float = line.GetLength();
var newLineObject = new LineObject();
newLineObject.SetName(line.name);
newLineObject.SetID(id);
newLineObject.SetStart(startPoint);
newLineObject.SetEnd(endPoint);
newLineObject.SetLength(lineLength);
return newLineObject;
}
Any help would be greatly appreciated! Cheers and hats off to the CommUnity!
Odd - the code you've posted shouldn't have that effect. Something weird in one of the getters/setters?
The getters and setters never reference the array. Here is the only method with write access to this array:
public function SetSnapLocationOccupied (rowID : int, colID : int, owner : String) {
// make sure we're not requesting an occupied spot
if (snapToLocation[rowID][colID] != "available") return;
snapToLocation[rowID][colID] = owner;
// if center location, don't allow anything else on this row
if (colID == 1) {
snapToLocation[rowID][0] = "occupied";
snapToLocation[rowID][2] = "occupied";
}
}
I suspect that ins$$anonymous$$d of manipulating the same array, each object is doing the same changes at the same time; giving the effect of each object manipulating the same array.
To confirm my suspicion, I would have to see how and where the LineObject class is used.
Common culprits are On$$anonymous$$ouse???(), OnTrigger/Collision???() and Update(), where you don't specify the object which should perform the action, so they all do it.
Creating instances...
// from aforementioned "elsewhere" script
static var testObject = new LineObject();
//... skip, skip, skip to my code ...
static function EditingComplete() {
// setup data storage
var startPoints = new Array();
var endPoints = new Array();
var angleArray = new Array();
var lengthArray = new Array();
// create line object and reference its data
for (var i : int = 0; i < lineArray.length; i++) {
// create new line object
var testObject = CreateLineObject(lineArray[i], i);
// store
startPoints[i] = testObject.GetStart(); // get start point
endPoints[i] = testObject.GetStart(); // get end point
angleArray[i] = testObject.GetAngle(); // calculate angle
lengthArray[i] = testObject.GetLength(); // calculate length
}
// ...
}
// NOTE: lineArray[#] is a VectorLine object