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 eeveelution8 · Jul 10, 2014 at 11:08 PM · arraybooleanmanagerindex

Turn based games turn manager is spazzing out

I have a game with several units on the field, some computer controlled, some player controlled. when the unit's AP reaches 0, a boolean called 'isitmyturn' is turned off. and the script should make the next unit's 'isitmyturned' boolean enabled in the queue.

However, as soon as the first unit's turn ends, the index value immediately sets itself out of the arrays index range. I'm not sure where it is going wrong, here is the script,

 var order : GameObject[];
 
 var selectedunit : GameObject;
 var index : int = 1;
 
 var numberofunits : int = 2;
 
 
 
 function nextturn(){
 
 if(selectedunit.tag == "Enemy"){
 selectedunit.GetComponent(enemyAI).isitmyturn = true;
 }
 if(selectedunit.tag == "Player"){
 selectedunit.GetComponent(player_script).isitmyturn = true;
 }
 index += 1;
 }
 
 
 
 
 
 
 function Update(){
 
 var selectedunit : GameObject = order[index];
 
 if(index > (numberofunits-1)){
 
 index = 0;
 }
 
 if(selectedunit.tag == "Enemy"){
 if(selectedunit.GetComponent(enemyAI).isitmyturn == false){
 
 nextturn();
 
 }}
 
 if(selectedunit.tag == "Player"){
 if(selectedunit.GetComponent(player_script).isitmyturn == false){
 
 nextturn();
 
 }}
 
 }
 


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 Kiwasi · Jul 11, 2014 at 12:29 AM

You have two declarations for selectedunit (line 3 and 28). This is normally bad. I would suggest changing line 28 to the following.

 selectedunit = order[index];

Not 100% sure this is the problem, I work in C# and it would give me a complier warning if I tried to write as you have done. Not sure if JavaScript responds the same.

Comment
Add comment · Show 2 · 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 Kiwasi · Jul 11, 2014 at 12:35 AM 0
Share

Just done some more research and I'm sure this is your problem. The declaration on line 28 is hiding the declaration on line 3.

However the variable on line 28 is local scope only, so your nextturn method checks the class variable. In essence the nestturn method always thinks its still the first turn.

Its not overly important to understand why, just be aware that you should only ever declare a variable with var once in any given script.

avatar image eeveelution8 · Jul 11, 2014 at 10:12 PM 0
Share

I realized I had 'var selectedunit' was written twice like you said, I should have noticed that, I changed it, and a few other things around and it works fine, thanks

avatar image
1

Answer by zelyn · Jul 11, 2014 at 02:21 AM

Short answer: set your starting index to 0.

JavaScript arrays have a 0 based index which means that the first object should have an index of 0.

In you code you state that the total number of objects is 2. This means that in your array you'd have an object at index 0 and an object at index 1. If you set your starting index as 1 in your logic then that means you are selecting the second object in the array. As soon as you increment the index you are targeting and try to select that object you get the index out of range exception.

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 Kiwasi · Jul 11, 2014 at 02:27 AM 0
Share

Good pick. If this is the only problem it could be solved by changing the index to start at 0.

You also need to move line 28 down to line 34. You are checking if the index is in range after you have tried to access it.

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

23 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

How to circumvent this strange boolean array behaviour? 1 Answer

Saving 30 bools in an array possible? 2 Answers

Array index is out of range error Edited 1 Answer

Array Index out of range? 1 Answer

[SOLVED] Problem with "foreach". 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