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 BeB_Wir3 · Apr 15, 2013 at 08:35 PM · arrayinspectorstatic

Static Array

hey people,

i'm trying to create an array with transforms which i can edit or read through another script. to do this i got to make it a static. but when i type static infront of the variable, it disapeares in the inspector, which is normal. but now i want to shove some transforms into this array via the inspector, but i cant cause its a static!

how do i fix this?

script i'm using:

     public Transform[] MeleeWeaponCase;



bunch of thanks.

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

2 Replies

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

Answer by Negagames · Apr 15, 2013 at 08:39 PM

You could create another array that isn't static, put the transforms in that array and then run:

 for(int i = 0; i < NonStaticMeleeWeaponCase.Length; i++){
     MeleeWeaponCase[i] = NonStaticMeleeWeaponCase[i];
 }

P.S I'm not a master as C# (I just started learning yesterday), so sorry in advance if there are any errors in that.

Comment
Add comment · Show 3 · 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 whydoidoit · Apr 15, 2013 at 08:41 PM 0
Share

It's an idea and would work (if you'd resized the array first) - but you are much better off with a singleton.

avatar image BeB_Wir3 · Apr 17, 2013 at 10:22 AM 0
Share

this worked for me, but i had to use

 $$anonymous$$eleeWeaponCase = NonStatic$$anonymous$$eleeWeaponCase;


because the for didn't work, and this is faster.

avatar image whydoidoit · Apr 17, 2013 at 11:05 AM 1
Share

So this is an ok solution to your problem (far better than the answer posted) - but begs the question why bother having configuration components copying to statics in the first place. It feels like your architecture is fighting against the engine you are building it in.

Let's say that at some point you decide you want to swap out a whole series of settings of your static arrays - using a singleton pattern this would be a single assignment and would guarantee consistency and compatibility - having multiple arrays being copied to will not.

So you might say Oh I've only got one array - and if that is always true then it's fine - but software tends to change and I would strongly counsel against learning practices that can lead to spaghetti code later if your design requirements suddenly need another array, or variable or any setting.

Settings for a game should never be stored directly in statics and should always be stored through singletons that allow you to swap and test other configurations with $$anonymous$$imal code impact.

avatar image
1

Answer by whydoidoit · Apr 15, 2013 at 08:40 PM

Rather than make the array static you should make a singleton - so you have one of these in the scene but a way of finding it quickly:

    public static YourScript instance;

    public Transform[] MeleeWeaponCase;

    void Awake()
    {
         instance = this;
    }

The from elsewhere

   var weapon =  YourScipt.instance.MeleeWeaponCase[0];
Comment
Add comment · Show 5 · 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 BeB_Wir3 · Apr 17, 2013 at 09:44 AM 0
Share

i've done some reseach on the singleton, and this is accualy a very good salution to my problem. so i spoke with one of my $$anonymous$$chers about it, and it apears this makes all of the info in the script static, and when you use this a lot, then lots of lagg will appear. so he recommended the other awnser. still bunch of thanx for your awnser and fast reply!

avatar image whydoidoit · Apr 17, 2013 at 10:59 AM 0
Share

I'm afraid I don't agree with your $$anonymous$$cher.

You only have one piece of information in the script which is the array you are using. Copying it to a static creates spaghetti code and is unnecessary. Nothing in my answer would ever cause your game to lag.

avatar image BeB_Wir3 · Apr 17, 2013 at 10:09 PM 0
Share

hmm. that sounds true. maybe i'll use it after all. sory if i offended you.

avatar image whydoidoit · Apr 17, 2013 at 10:16 PM 0
Share

Nope you didn't offend me :) And I'm very sorry if I sounded offended!

Just trying to offer my best advice - I know, because it has happened to me - that statics need to be limited and that a singleton pattern fits with your requirements.

avatar image Dracorat · Apr 17, 2013 at 10:18 PM -1
Share

Either you didn't explain the situation to your $$anonymous$$cher well, or your $$anonymous$$cher is an idiot.

Personally I don't feel that static references create spaghetti code even if you need to test other configurations, but I can't argue with the wisdom in using a Singleton either. So do it!

Singletons have an advantage over pure static classes in that when the instance is initialized you can set up default values that you might not be able to directly because they rely on computed values.

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

14 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

Related Questions

GameObjects static array NullReferenceException 1 Answer

How to place a non monobehaviour script into an array in inspector 1 Answer

How to get a List of Color Arrays? 2 Answers

Display custom names for array 1 Answer

GUIStyle in an inspector array 0 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