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 Ebil · May 02, 2012 at 10:36 AM · c#arrayclass

Creating a class wide array (c#)

Hi everyone,

I have to create an array that i can use in in several functions, but if i want to create it I get an error

 public class PosChanged : MonoBehaviour {

 private int changeCount = 0;
 private int breakForce = 20;
 Vector3[] v = new Vector3[transform.childCount];
 Vector3[] v_old = new Vector3[transform.childCount];
 .....

I know I would have to do it in Start() because of the transform.childCount, but I cant access it then in FixedUpdate() if its created in Start().

So how do I do that?

Errormsg:

(8,35): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.transform'

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

1 Reply

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

Answer by Kryptos · May 02, 2012 at 11:15 AM

You have to declare the arrays as class members, but you initialize them in Start:

 public class PosChanged : MonoBehaviour {
 
     private int changeCount = 0;
     private int breakForce = 20;
     Vector3[] v;
     Vector3[] v_old;

     void Start()
     {
         v = new Vector3[transform.childCount];
         v_old = new Vector3[transform.childCount];            
     }

Then you can access them inside other methods such as FixedUpdate:

     void FixedUpdate()
     {
         v[0] //...
     }
Comment
Add comment · Show 4 · 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 Bunny83 · May 02, 2012 at 11:28 AM 1
Share

Exactly, the problem is that transform.childCount is a property which executes a function on the transform component, but the field initializers are executed even before the constructor. so you have no access to other stuff there and you couldn't execute class functions there, only static stuff because static variables are initialized first. transform is a non static property.

avatar image Owen-Reynolds · May 02, 2012 at 01:24 PM 0
Share

Even global int a=8; int[] B = new int[a]; will error out, complaining about non-static a.

avatar image Ebil · May 03, 2012 at 06:24 AM 0
Share

funny how I had exactly this yesterday at school ;D Thanks.

avatar image Rob Porter · May 10, 2014 at 01:27 PM 0
Share

I have been looking for this for ages. Thanks!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Incrementing a String Array 1 Answer

Why is Start() from my base class ignored in my sub-class? 4 Answers

Need help setting an array of my class 0 Answers

C# sub Values/Arrays for a position Array 0 Answers

How can I call a function of a class array from another function of a class 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