Question: “What have I done wrong in my type casting which results in this error?”
Hey Unity Gurus. I have been using Unity and UnityScript for about 6 months now. This forums are awesome and nearly every question I’ve ever had were covered and covered in detail. One issue keeps kicking my butt though so here’s my first question to the Gurus of Unity.
I get a ‘foo’ is not a member of ‘Object’ error with #pragma strict enabled. Without it, my code works just fine but I really want to fix it to learn WHY I’m messing this up. Below is my code. If I’m WAY off the mark in my execution of this please feel free to rip me a new one.
foo being ‘name’, ‘distance’, ‘health’, ‘object’, ‘carrier’, ‘size’
--------------the code-------------
class TargetData { // Defines the ‘class’ to be used as an Object
var object : GameObject;
var distance : int;
var health : float;
var name : String;
var carrier : boolean;
var size : float;
} // END TargetData
var sensorHits = new Array();
var targetObject = new Array(); // Array for Target Object Reference
function SensorManager () {
var layerMask = 1 << thisShip.layer; // Bit Slide to the current ships Layer Mask
layerMask = ~layerMask; // Inverse the LayerMask to everything NOT this Layer
sensorHits = Physics.OverlapSphere(transform.position, detectionChance, layerMask); // Ping
for (var n = 0; n < sensorHits.length; n++) { // Builds the Multiple Objects in the Array for Target Data
targetObject[n] = TargetData();
targetObject[n].name = sensorHits[n].name;
targetObject[n].distance = Vector3.Distance(transform.position, sensorHits[n].transform.position);
targetObject[n].health = sensorHits[n].GetComponent(Ship_Status_Manager).structuralIntegrity;
targetObject[n].object = sensorHits[n].gameObject;
if(sensorHits[n].GetComponent(Ship_Status_Manager).isCarrier) {targetObject[n].carrier = true;}
targetObject[n].size = sensorHits[n].GetComponent(Ship_Status_Manager).shipLength;
} // End FOR
I'm not sure why the problem you're having showed itself as #pragma Strict errors, because the line "var sensorHits = new Array();" shouldn't compile even without #pragma Strict in the first place, because the Array class is abstract in .Net. Maybe that was actually the root of the problem the entire time? You can't instantiate objects of abstract classes. System.Array is the base class all other arrays inherit from, and it's there to expose a series of static methods common to all arrays, such as sorting and copying.
– CHPedersen