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 zcolek · Dec 21, 2012 at 12:53 AM · guilistclassstaticoffset

Static class member for offset

I have a public class for my items, and List for that items which is empty at the begining.

Ech time I click on object on screen ("GUITexture"), I instantiate a itemList Class and add it to a item List, then I render them on GUI from for loop.

My constructor for itemList class needs two parametars 1-name and 2-texture.

Everything works, adding to list, displaying from list, draging from list... but the order on screen is not good!

I tryed with static float for my X position, and each time the class is instantiated I want it to increase by 60;

I make it default 10, but when I print that default value I get 360???

I want to make dynamic offset of X value for Rect for each new class instance...

here is the code for class:

 [System.Serializable]
 public class itemsList{    
     public bool drag;
     public string name;
     //GUITexture textura;
     public static float pX = 10;
     public Texture myTexture;
     public float pY;
     public float width;
     public float height;
     public Rect myRect;
     public itemsList(){}
     public itemsList(string ime, Texture slika){
         name = ime;
         drag = false;
         width = 50;
         height = 50;
         myTexture = slika;
         pY = 50;
         myRect = new Rect(itemsList.pX,pY,width,height);
         itemsList.pX += 60;
         return;
     }    
 }
Comment
Add comment · Show 6
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 Shrandis · Dec 21, 2012 at 09:25 AM 0
Share

It looks like you should be using pX only in itemsList class and there should be no reason to make it "public". Do you use it in some other place? Are you absolutely sure that you are only increasing it in the constructor?

avatar image zcolek · Dec 21, 2012 at 09:30 AM 0
Share

I made it public just to chek it's value. When I make a static value in a class, that value is used for all Instances of class? And that value because it's pubic I can acces and print even there are no instances of a class? I put it in a print in Update print(itemsList.pX); and I got a value of 360 not 10? And yes I am sure that I am increasing it only from class constructor, I dont pass any value to it when I make instance, only name andt texture... dont't understand...

avatar image Shrandis · Dec 21, 2012 at 09:49 AM 0
Share

You can access that value without an instance because it is static, not because it is public.

Are you sure you are printing the value BEFORE the itemsList constructors are called? $$anonymous$$aybe you call print(itemsList.pX) after the itemsList objects are initalized and pX is increased to 360?

avatar image zcolek · Dec 21, 2012 at 10:13 AM 0
Share

:) I done now a few tests... I have a list that storse itelList instances, it is empty on the begining, and I have few objects on stage and them have one script attached to make a new instace od itemList, i disabled that script and I have printed value of 10... sorry I didn't notice that I made instance in Awake and that was cousing the problem, now I put it in On$$anonymous$$ouseDown, and pritnig value of 10 and the order on screen is good... :) $$anonymous$$y BAD!!! I lost my self in the code... :( But thank you so much! You opend my eyes ;) And one quastion before you quit this Answer... Can I drag GUI.buttons outside of GUI.window... I make tham in Window Function... I drag them but they not visible outside window bounds... ;) one more time Thank you!

avatar image Shrandis · Dec 21, 2012 at 10:22 AM 0
Share

I'm glad I could help. About your second question, I haven't used GUI.window before. I've searched for a while but couldn't find an answer to your question, maybe you should make a separate question for that?

Also, since my previous comment solved your problem, I'm posting it as an "Answer" so you can accept it and close the question, please don't forget to do so :) You need to click the checkmark icon near the answer to accept it.

Show more comments

1 Reply

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

Answer by Shrandis · Dec 21, 2012 at 10:23 AM

You are printing the value AFTER all itemsList constructors are called. That's why your print(itemsList.pX) prints 360.

Check where you are creating the instances and where you are calling print(itemsList.pX).

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

10 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

Related Questions

C# List and GUI 3 Answers

A node in a childnode? 1 Answer

Displaying the players through GUI 1 Answer

Static GameObject List gives error when calling Add or Count from another script 1 Answer

An object reference is required to access non-static member 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