- Home /
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;
}
}
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?
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...
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?
:) 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!
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.
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).