- Home /
Accessing static variable
Hello, what i'm trying to do is add a object to a static array of transforms. The reason i'm doing this is because i made a grid of planes 68x68 grid. and when i click on a specific plane it will add it to the array. I know i create it like:
static var BList : Array[];
I'm just not sure how to add a object to it from my other script.
If you can help then thanks! :D
That's not how you create it actually. Array[]
would be a built-in array of Arrays (so, essentially a 2D array), and you never want to use the Array class anyway, since it's slow and untyped. Use a generic List of Transforms ins$$anonymous$$d.
Also, are you sure you want a static variable? Static means there is only one instance per class. If you're not sure, just use a standard public variable along with GetComponent.
Okay so something like:
public var BList : Transform[];
??
No, please read the link I posted. You can use Transform[] as a built-in array of Transforms, but you won't be able to add any items to it after you initialize it, since built-in arrays are fixed-size.
Okay so from what i understand, to declare i would do:
var BList = new List.<GameObject>();
And then to add a object it would be
BList.Add(theItem);
but i still have absolutely no idea how to do add something from another script... xD
http://lmgtfy.com?q=Unity+Access+objects+from+other+script
Before asking anything on Unity Answers
, check google first, as 99% of the time, someone else has asked the same question before you!
Answer by pborg · Jul 14, 2013 at 06:27 AM
how about something like this:
//static script-holds planes
static var grid : Transform[] = new Transform[68];
static var gridcount : int = 0;
function Add(item : transform)
{
if(gridcount < grid.length)
{
grid[gridcount] = item;
gridcount++;
}
}
...
//any other script
//this one is more pseudo code
function Onclick()
{
//use raycast rayhit system to identify temp-you can find on this site
var temp : Transform = clickedobject;
staticscriptname.Add(temp);
}
I hope this helps, to give you an example. Anybody feel free to comment if more needs to be added for the execution. If a variable is static you only need to reference the name of the script. Add could even be in the any other script and referencing grid & gridcount as staticscriptname.grid & staticscriptname.gridcount respectively. Anybody feel free to comment on my post if you think I missed something.
Okay Thanks for the post, though i'm still not sure what this is suppose to do:
grid[gridcount] = item;
@uberokeer Just so you know, you can't expect someone to get back to you within 3 hours, especially if the timezones obviously don't match up. Please refrain from posting anything like: "Hello?" or "Help please", because all it does is piss people off :)
@Benproductions1 Hey dude, if You don't have the answer than thumb down this question and $$anonymous$$ove on. And one tip... Stop Trolling.
that's just adding item to the array. This a stack based implementation of an array. gridcount is the counter (starting at 0) where the item is being added inside grid. item is the transform that is passed into the function from the other script. Benproductions1 isn't trolling though; Sorry I didn't get back to you sooner.
Your answer
