Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AngryMarine · Aug 29, 2011 at 01:15 AM · javascriptobjectclass

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
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AngryMarine · Aug 29, 2011 at 03:31 AM 0
Share

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.

avatar image CHPedersen · Aug 29, 2011 at 07:09 AM 0
Share

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.

avatar image AngryMarine · Aug 29, 2011 at 07:20 AM 0
Share

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!!!

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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];
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges