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 Myhijim · Aug 01, 2012 at 08:03 AM · arrayclasseof

Arrays In Classes?

I have a class within unity, however it will not allow me to define arrays.

This is my class:

 class WeaponInfo
 {
  //Name/Model
  var name = "";
  var model:Mesh;
  
  //FireRate
  var frn:int;
  var fr = frn;
  
  //ReloadRate
  var rrn:int;
  var rr = rrn;
  
  //Ammo
  var maxMags:int;
  
  var ammo = new int[4];
  ammo[0] = 0;
  ammo[1] = 0; //Here is the problem
 
 }

 

I get the errors:

Error BCE0044: expecting }, found 'ammo'. (BCE0044) (Assembly-UnityScript)

Error BCE0044: expecting EOF, found '}'. (BCE0044) (Assembly-UnityScript)

It works complketely fine when i remove the bottom two ammo variables, so i dont know what the problem is.

Any help is appreciated, thanks!

Comment
Add comment · Show 2
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 fafase · Aug 01, 2012 at 08:07 AM 0
Share

I am not 100% sure of the details but I know there is something like you cannot assign values to a staticly instantiated array (static as opposed to dynamic). You have to assign those values outside the class.

avatar image Myhijim · Aug 01, 2012 at 08:19 AM 0
Share

Is there any way to assign arrays within the class? Thanks for the help

2 Replies

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

Answer by Eric5h5 · Aug 01, 2012 at 08:22 AM

You should only define the types of the variables outside of the constructor. You can do whatever manipulation you need in the constructor:

 ...
 var ammo : int[];

 function WeaponInfo () {
     ammo = new int[4];
     ammo[0] = 0; // not necessary, it's 0 by default anyway
 }

Likewise, instead of this:

 var frn:int;
 var fr = frn;

Do this:

 var frn : int;
 var fr : int;

Use the constructor as necessary. Also, do yourself a big favor and use legible variable names; "frn" and "fr" don't mean anything by themselves, and you WILL later wonder what they mean. Just write it out: "fireRate". You don't gain anything by using cryptically short variable names. If you find yourself writing comments to explain them, that's a huge warning sign. Code should be self-documenting as much as possible.

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 Myhijim · Aug 01, 2012 at 08:38 AM 0
Share

See the problem with assigning them outside the function is another extrerior array... var weapon = new WeaponInfo[3];

Im startting to think just using variables ins$$anonymous$$d of arrays will be easier and more ledgible, thanks for your time however :)

avatar image Eric5h5 · Aug 01, 2012 at 08:42 AM 0
Share

Eh, it's your funeral. I think you'd be better off understanding how classes and constructors work though. I have no idea what you mean by "assigning them outside the function is another extrerior array".

avatar image Myhijim · Aug 01, 2012 at 08:50 AM 0
Share

I sorta do understand how classes work. Im currently upgrading my code from $$anonymous$$ANY $$anonymous$$ANY $$anonymous$$ANY arrays and variables to Classes ins$$anonymous$$d.

Thank you anyhow, i have fixed it all now :)

avatar image fafase · Aug 01, 2012 at 09:59 AM 0
Share

I may be wrong but IU think you misunderstand the principle of constructor. A constructor is a special function inside a class that is launched when you create an instance of a class (object). The constructor does not return any value and is named the same as the class.

 class WeaponClass{
     var ammo = new int[4];
     function WeaponClass(){ 
         ammo[0] = 0;
         ammo[1] = 5;
         ammo[2] = 10;
         ammo[3] = 15;
     }
 }

 function Update(){
     var weapon = new ClassWeapon()
 }

Now your object has the appropriate values in the array. You do not need to call the function as it is called automatically with new.

avatar image
0

Answer by tobiass · Aug 01, 2012 at 08:27 AM

You can define a variable in the class scope and directly initialize it. Assign values in methods. A static initialization method (matches the name of the class) is called when the class is instanced:

 function WeaponInfo()
 {
     ammo[0] = 0;
     ammo[1] = 0; // not needed. initialized to 0 anyways
 }
Comment
Add comment · 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

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Creating a class wide array (c#) 1 Answer

Issue with some GUITextures and an Array [SOLVED] 1 Answer

String.Split problem 1 Answer

C# Incrementing a String Array 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