x


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
more ▼

asked Aug 29 '11 at 01:15 AM

AngryMarine gravatar image

AngryMarine
76 1 2 4

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. Instead 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.

Aug 29 '11 at 03:31 AM AngryMarine

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.

Aug 29 '11 at 07:09 AM CHPedersen

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. Much Appreciated!!!

Aug 29 '11 at 07:20 AM AngryMarine
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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];
more ▼

answered Aug 30 '11 at 07:53 PM

AngryMarine gravatar image

AngryMarine
76 1 2 4

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1123
x351
x303
x1

asked: Aug 29 '11 at 01:15 AM

Seen: 931 times

Last Updated: Aug 30 '11 at 07:53 PM