Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 The_r0nin · Jun 09, 2010 at 07:47 PM · javascriptarrayclasses

Array of custom class objects all return the same value?

I'm having trouble with a system of creating waypoints and the way that my custom waypoint class is being referenced. My code is as follows:

in a file called Waypoints attached to my Player

public var location: Vector3; public var attitude: Quaternion; public var pathType:int;

function SetWayPoint(tr_Loc:Vector3,tr_Rot:Quaternion,pT:int){ location = tr_Loc; attitude = tr_Rot; pathType = pT; }

in a file called PathCreate attached to my Player

var wayPts: Waypoints[] = new Waypoints[21];

function FirstMove() { wayPts[0].SetWayPoint (transform.position,transform.rotation,1); wayPts[1].SetWayPoint (transform.position,transform.rotation,2); wayPts[2].SetWayPoint (transform.position,transform.rotation,3);

print(wayPts[0].pathType+" "+wayPts[0].pathType+" "+wayPts[0].pathType); }

If I run the program and call "FirstMove()", I get a "NullReferenceException: Object reference not set to an instance of an object"

If I go to the Player in the Inspector and select the PathCreate script, then select the elements of "Way Pts" and change them from "None (Waypoints)" to "Player (Waypoints)" [based on the pull-down], the program runs with no error, but my printed output is "3 3 3" instead of the expected "1 2 3". I'm guessing this means that all of the waypoints in the array are writing to the single waypoint data from the script, but I don't know how to prevent this. Any help would be appreciated! Thanks!

Comment
Add comment · Show 1
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 The_r0nin · Jun 09, 2010 at 08:26 PM 0
Share

Sorry, I edited the above to add the "pathType" component of Waypoints so that my problem would be more clear.

1 Reply

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

Answer by Tetrad · Jun 09, 2010 at 08:03 PM

Initializing the array of WayPoints doesn't initialize each instance of Waypoints. So after new Waypoints[21] is called, you have an array of size 21 that points to 21 null Waypoints objects.

Now if Waypoints has to be a MonoBehaviour you're kind of out of luck unless you hook things up in the editor, but if you just want your Waypoints class to be a thin wrapper for position and rotation data you could just make it a c# struct so you don't have to do anything (all value types get initialized to zero). Or you could make it a c# class, but you'd have to new off each instance of it like this:

wayPts[0] = new WayPoints();
wayPts[0].SetWayPoint( /*whatever*/ );

I don't know how you'd go about doing that in Javascript, though, since all classes are assumed to derive from MonoBehaviour.

Edit: If you want to add a bunch of waypoint objects, do this:

wayPts[0] = AddComponent( "WayPoints" );
wayPts[0].SetWayPoint( /*whatever*/ );
Comment
Add comment · Show 6 · 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
avatar image The_r0nin · Jun 09, 2010 at 08:11 PM 0
Share

Thanks! I'm going to try and stick with Java for the moment. I'll see if anyone knows how to hook up each instance in JavaScript, or, more importantly, how to get each instance to register when I attempt to fill it via its index.

avatar image Tetrad · Jun 09, 2010 at 09:15 PM 0
Share

Actually here's what you need to do:

wayPts[0] = AddComponent( "WayPoints" ); wayPts[0].SetWayPoint( whatever );

avatar image The_r0nin · Jun 09, 2010 at 09:31 PM 0
Share

I tried the AddComponent, but I got an 'AddComponent' is not a member of 'Waypoints' error. I haven't extended Waypoints from anything (unless it is the automatic extension from $$anonymous$$onoBehavior), so maybe this is why...

avatar image Tetrad · Jun 09, 2010 at 10:02 PM 0
Share

Are you sure you used it correctly? You shouldn't be doing wayPts[0].AddComponent. I edited my original post.

Also by default, your classes made in javascript all extend from $$anonymous$$onoBehaviour.

avatar image The_r0nin · Jun 09, 2010 at 10:09 PM 0
Share

I did it exactly like you posted: wayPts[0] = AddComponent("Waypoints"); wayPts[0].SetWayPoint (...

I also tried it the other way, and I tried it with a GameObject.AddComponent as well. All of them choke on the "AddComponent"...

Show more comments

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

No one has followed this question yet.

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Instantiate into array : Out of range? 1 Answer

Can i use objects from a builtin Array to populate a Js Array 2 Answers


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