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 robinking · Aug 05, 2010 at 06:40 PM · javascriptclassmethod

Custom class functions - accessing one instance changes them all?

I have put the following code in a script:

class Panel {

 public static var cornerPos = new Vector3[3];

 function Panel(){}

 function Make( x0 : float, y0 : float, z0 : float, x1 : float, y1 : float, z1 : float, x2 : float, y2 : float, z2 : float ) {
     cornerPos = [ Vector3(x0,y0,z0), Vector3(x1,y1,z1), Vector3(x2,y2,z2) ];
 }

}

function Blueprint() { var p = 6; panels = new Panel[p]; for (var ps=0;ps<p;ps++) { panels[ps] = new Panel(); }

 panels[0].Make( 0,0,0,  0,6,0,  5,0,2 );
 panels[1].Make( 0,6,0,  5,4,2,  5,0,2 );
 panels[2].Make( 5,0,2,  5,4,2,  8,0,-1 );
 panels[3].Make( 5,4,2,  8,5,-1, 8,0,-1 );
 panels[4].Make( 8,0,-1, 8,5,-1, 14,0,1 );
 panels[5].Make( 8,5,-1, 14,7,1, 14,0,1 );

}

So the Panel class has an array (length=3) of Vector3's, one for each corner of the panel. The idea of the Blueprint() function is to make 6 instances of the Panel class and then set the Panel.cornerPos[] values. 6 instances of the class are being created fine, BUT - they all have the same cornerPos[] values. I've also tried it without the Make() method thus:

panels[0].cornerPos[0] = Vector3( 0,0,0);
panels[0].cornerPos[1] = Vector3( 0,6,0);
panels[0].cornerPos[2] = Vector3( 5,0,2 );

And the result is the same - just accessing one instance sets the values for the other instances. What am I doing wrong?

Comment
Add comment
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

1 Reply

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

Answer by Mike 3 · Aug 05, 2010 at 06:43 PM

You're using a static variable to store the data - that'll be shared between all instances of the class

If you remove static, it should work (Though seeing as it's public too, hopefully you're not using it outside the class for anything)

Comment
Add comment · Show 2 · 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 robinking · Aug 05, 2010 at 07:14 PM 0
Share

Perfect!

Why not use the variable outside of the class? Can I not have a line somewhere like... panels[0].cornerPos[0] = Vector3(0,0,0); ? It seems to work fine, is that not a good idea?

avatar image Mike 3 · Aug 05, 2010 at 08:27 PM 0
Share

That's fine - I was thinking more along the lines of using it as a static with Panel.cornerPos[0] = whatever;

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

Public Class Error 1 Answer

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

javascript call a method from c sharp and vise versa 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