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 crag · Feb 26, 2014 at 06:59 AM · javascriptarrayprivateclass instance

Private Array in custom Class behaving Public

Hey Unity peeps,

I have a custom class that can have any number of instances generated at runtime. I am building a very literal "snapping" matrix (multi-dimensional, jagged array) specific to each instance of the class. The issue is that all instances of the class seem to be manipulating the same array instead of their own instance of said "private" array (snapToLocation).

 // da class
 class LineObject {
 
     private var name : String;
     private var id : int;
     private var startPoint : Vector2;
     private var endPoint : Vector2;
     private var lineLength : float;
 
     private var maxLength : int = 12;
     private var snapToLocation = Array(maxLength);
 
 
     public function LineObject () {
 
         // prep item "snapping" matrix
         for (var i : int = 0; i < maxLength; i++) {
             snapToLocation[i] = new Array(3);
             snapToLocation[i][0] = "available";
             snapToLocation[i][1] = "available";
             snapToLocation[i][2] = "available";
         }
 
     }
 
     // getters, setters, etc...
 
 }


 // and elsewhere ... da instantiation
 function CreateLineObject (line, id) : LineObject {
 
     var vectorPoints : Vector2[] = line.points2;
     var startPoint : Vector2 = vectorPoints[0];
     var endPoint : Vector2 = vectorPoints[1];
     var lineLength : float = line.GetLength();
 
     var newLineObject = new LineObject();
     newLineObject.SetName(line.name);
     newLineObject.SetID(id);
     newLineObject.SetStart(startPoint);
     newLineObject.SetEnd(endPoint);
     newLineObject.SetLength(lineLength);
     
     return newLineObject;
 
 }


Any help would be greatly appreciated! Cheers and hats off to the CommUnity!

Comment
Add comment · Show 7
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 whydoidoit · Feb 26, 2014 at 07:02 AM 1
Share

Odd - the code you've posted shouldn't have that effect. Something weird in one of the getters/setters?

avatar image crag · Feb 26, 2014 at 04:35 PM 0
Share

The getters and setters never reference the array. Here is the only method with write access to this array:

 public function SetSnapLocationOccupied (rowID : int, colID : int, owner : String) {
 
     // make sure we're not requesting an occupied spot
     if (snapToLocation[rowID][colID] != "available") return;
 
     snapToLocation[rowID][colID] = owner;
 
     // if center location, don't allow anything else on this row
     if (colID == 1) {
         
         snapToLocation[rowID][0] = "occupied";
         snapToLocation[rowID][2] = "occupied";
 
     }
 
 }
 
avatar image Jamora · Feb 26, 2014 at 06:12 PM 0
Share

I suspect that ins$$anonymous$$d of manipulating the same array, each object is doing the same changes at the same time; giving the effect of each object manipulating the same array.

To confirm my suspicion, I would have to see how and where the LineObject class is used.

Common culprits are On$$anonymous$$ouse???(), OnTrigger/Collision???() and Update(), where you don't specify the object which should perform the action, so they all do it.

avatar image perchik · Feb 26, 2014 at 06:14 PM 0
Share

How do you instantiate new instances of that class?

avatar image crag · Feb 26, 2014 at 08:20 PM 0
Share

Creating instances...

 // from aforementioned "elsewhere" script
 static var testObject = new LineObject();
 
 //... skip, skip, skip to my code ...
 
 static function EditingComplete() {
 
     // setup data storage
     var startPoints = new Array();
     var endPoints = new Array();
     var angleArray = new Array();
     var lengthArray = new Array();
     
     // create line object and reference its data
     for (var i : int = 0; i < lineArray.length; i++) {
         
         // create new line object
         var testObject = CreateLineObject(lineArray[i], i);
         
         // store
         startPoints[i] = testObject.GetStart();        // get start point
         endPoints[i] = testObject.GetStart();        // get end point
         angleArray[i] = testObject.GetAngle();        // calculate angle
         lengthArray[i] = testObject.GetLength();    // calculate length
 
     }
 
     // ...
 
 }
 
 // NOTE: lineArray[#] is a VectorLine object
 
Show more comments

0 Replies

· Add your reply
  • Sort: 

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Physics.OverlapSphere with a min radius? 1 Answer

expected. Insert a semicolon at the end. When the end that it says, is a } Could someone help us fix this? 2 Answers

Javascript - find a value in an array 0 Answers

Array Index out of range (JavaScript) 1 Answer

have array target person of most priority/stick with top prior 0 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