- Home /
Class Casting US/JS - 'foo' is not a member of 'Object'
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
Ok I think I figured something out but I would love it if someone who actually knows could ellaborate or assist. I think it's because UnityScript can't define the array type like I'm intending. Ins$$anonymous$$d I decided to try the .NET BuiltIn arrays for grins.
Simply changing the array has resolved this problem quite well. Here's what I believe is the solution.
var targetObject : TargetData[];// Array for Target Object Reference
targetObject = new TargetData[sensorHits.length];
Everything is now working well and #Pragma Strict is happy. I'm not sure I understand WHY though so if someone could explain why I'd love to give them credit for their answer.
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. $$anonymous$$aybe 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.
That's the craziest part. Not only did it compiled but it worked perfectly. I used OnGUI calls to display the variables and they did indeed register the value as it should have. As whole objects. Only when I added #pragma strict did the compiler catch it.
I read about how Javascript arrays have no set size which was why I was using it. It never occured to me use the BuiltIn arrays and just redefine them each pass which resolved my problem. That's why I posted it. Because the crazy thing was that it worked in game. I just knew it wasn't a good way to program it and probably unstable.
Thank you for explaining System.Array I didn't know that. $$anonymous$$uch Appreciated!!!
Answer by AngryMarine · Aug 30, 2011 at 07:53 PM
Well here is the answer for other to reference. Replace the variable length javascript array with the BuiltIn fixed length arrays. Redefine the array length each time the array is edited and updated via the below changes to the original code.
var targetObject : TargetData[];// Array for Target Object Reference
targetObject = new TargetData[sensorHits.length];
Your answer
Follow this Question
Related Questions
Extending a class 1 Answer
Getting any type in Unity Inspector 1 Answer
How do I Create 5 Random Racers? 2 Answers
How to store a class as a variable 1 Answer
UnityScript - Class reference 1 Answer