Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Emanrice · Jun 27, 2011 at 09:33 PM · arraysbce0049

Giving the player ammo

Hi, I have a script called "Stats" that setups the games variable such as the health, ammo, etc. The script:

 static var statHP : float = 100;
 static var statMaxHP : float = 100;
 static var gunAmmo = new Array();
 function Awake(){
     gunAmmo[0] = 10;
     gunAmmo[1] = 50;
 }

I'm not sure if I set up the array correctly... Anyway, before creating/firing a bullet, I check the ammo count for the current weapon. Each weapon has a variable gun which I use to check the ammo count. The script for creating the bullet(just a part):

 if (Input.GetButton("Fire1") && Stats.gunAmmo[gun] > 0 && timer == 0){
     Instantiate(bullet,transform.TransformPoint(Vector3(0,0,zOff)),transform.rotation);
     Instantiate(gunfire,transform.TransformPoint(Vector3(0,0,zOff)),transform.rotation);
     timer = wait;
     Stats.gunAmmo[gun] --;
 }

I use that and I get the error:

 Assets/Scripts/Create bullet.js(12,30): BCE0049: Expression 'Stats.gunAmmo.get_Item(self.gun)' cannot be assigned to.

So I'm guessing either you can't do anything with arrays outside of the script that contains it, or I created the array incorrectly. Could any help me please?

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
1
Best Answer

Answer by flaviusxvii · Jun 27, 2011 at 09:40 PM

http://unity3d.com/support/documentation/ScriptReference/Array.html

Did you give your array a size (or length)?

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 Emanrice · Jun 27, 2011 at 09:46 PM 0
Share

I've tried putting gunAmmo.length = 2; inside Awake, but it still gave me the same error.

avatar image flaviusxvii · Jun 27, 2011 at 10:02 PM 1
Share

Hmm.. maybe this would be better.

static var gunAmmo = new int[2];

avatar image Emanrice · Jun 27, 2011 at 10:06 PM 0
Share

It doesn't give me an error or anything, but now for some reason the weapon doesn't create/fire bullets anymore... I'm assu$$anonymous$$g this is how I should set it up?

static var gunAmmo = new int[2];

function Awake(){
gunAmmo[0] = 10; gunAmmo[1] = 50; }

avatar image flaviusxvii · Jun 27, 2011 at 10:23 PM 0
Share

That looks good, so I don't think the issue is your array anymore.

Side note: Tracking ammo in a global variable is a little strange. Wouldn't it make more sense to have a "Gun" component with ammo as a datamember?

avatar image Emanrice · Jun 27, 2011 at 10:27 PM 0
Share

So it may just be my firing script? I'll go mess around with it.

I'm used to using arrays/global variables for ammo and stats, plus I'm needing to save and load the variables, so I'll be using global variables for now.

EDIT: By datamember I'm assu$$anonymous$$g variables in scripts(var blah : int;) right?

avatar image
0

Answer by Ludiares.du · Jun 27, 2011 at 09:52 PM

Try setting

 gunAmmo = new Array();

in the awake function.

Something like:

 function Awake(){
     gunAmmo = new Array();
     gunAmmo[0] = 10;
     gunAmmo[1] = 50;
 }
Comment
Add comment · Show 1 · 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 Emanrice · Jun 27, 2011 at 09:57 PM 0
Share

Doing that I get the error: Assets/Scripts/Create bullet.js(8,47): BCE0019: 'gunAmmo' is not a member of 'Stats'.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Getting C# to access a javascript global Array and Components 1 Answer

How would I call upon an asset in the projects tab with an array? 1 Answer

Beginner : Random gameobject from array 2 Answers

Arrays declaration c# 3 Answers

Converting a JS script into C# - hit a wall with arrays... 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