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 uberokeer · Jul 14, 2013 at 03:14 AM · javascriptstaticglobal array

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

Comment
Add comment · Show 5
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 Eric5h5 · Jul 14, 2013 at 04:03 AM 0
Share

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.

avatar image uberokeer · Jul 14, 2013 at 04:12 AM 0
Share

Okay so something like:

 public var BList : Transform[];

 

??

avatar image Eric5h5 · Jul 14, 2013 at 04:20 AM 0
Share

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.

avatar image uberokeer · Jul 14, 2013 at 04:34 AM 0
Share

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

avatar image Benproductions1 · Jul 14, 2013 at 05:50 AM 0
Share

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!

1 Reply

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

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.

Comment
Add comment · Show 7 · 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 uberokeer · Jul 14, 2013 at 08:31 PM 0
Share

Okay Thanks for the post, though i'm still not sure what this is suppose to do:

 grid[gridcount] = item;

   
avatar image uberokeer · Jul 14, 2013 at 10:54 PM 0
Share

Hello? Help please :P

avatar image Benproductions1 · Jul 15, 2013 at 12:16 AM 2
Share

@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 :)

avatar image uberokeer · Jul 16, 2013 at 02:37 PM 0
Share

@Benproductions1 Hey dude, if You don't have the answer than thumb down this question and $$anonymous$$ove on. And one tip... Stop Trolling.

avatar image pborg · Jul 16, 2013 at 06:01 PM 1
Share

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.

Show more comments

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

18 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

Related Questions

Compile priority - accessing static variables 1 Answer

BCE0019: 'distanceScore' is not a member of 'float'. (JavaScript) 3 Answers

Storing variables temporarily during static functions? 1 Answer

Static array with custom class? 1 Answer

Cannot use Static var/ Unknown Idientifier 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