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 Joshua M · Feb 07, 2013 at 09:21 AM · c#arrayfor-loopturn-based

Need help cycling through an array with a 'for' loop.

Ok, I am attempting to create a tile-based turn-based game. I am in the early stages of it and am trying to just get my fog-of-war / tile assignments to work. The way it should play is that you have scouts and you send them to a tile that is "fogged out" it sets a bool variable curScouting to true. Then when the turn is over (henchmen assigned to tasks) you hit the end turn button. I am trying to set up an array of strings to hold the tile-names (based on coordinates from center, center eg. -2 / -2). I then want the EndTurn script to step through each map to access the Fog_Tile script (on each tile) where I can check for the curScouting flag and set some more flags that result in the tile being revealed. I am running into errors cycling through things. The error message I get is "NullReferenceException: Object reference not set to an instance of an object EndTurn.OnMouseUp () (at Assets/Reclamation/EndTurn.cs:33)"

Here is my script: "using UnityEngine; using System.Collections; using System.Collections.Generic;

public class EndTurn : MonoBehaviour {

 public string[] Map = new string[] {"-2 / 2", "-1 / 2", "0 / 2", "1 / 2", "2 / 2", "-2 / 1", "-1 / 1", "0 / 1", "1 / 1", "2 / 1", 
     "-2 / 0", "-1 / 0", "Castle", "1 / 0", "2 / 0", "-2 / -1", "-1 / -1", "-1 / 0", "-1 / 1", "-1 / 2", "-2 / -2", "-1 / -2",
     "0 / -2", "1 / -2", "2 / -2"};
 private int mapTiles = 25;
 
 public Henchmen scouts;  // points to Henchmen script to manipulate scout henchy #
 public Fog_Tile scouting; // points to Fog tile script to manipulate fog flags


 void Start () 
 {
 scouts = GameObject.Find ("Castle").GetComponent();    
 
     
 }
 
 
 void OnMouseUp()
 {
 for (int i = 0; i < (mapTiles - 1); i++)    
     {
 
         scouting = GameObject.Find (Map[i]).GetComponent();            
         if (scouting.curScouting == true)
         {
         scouting.curScouting = false;
         scouting.scouted = true;
         scouts.scouts += 1;
         
         }
         
         
     }
     
 }
 
 
 void Update () {
 
 }
 
 
 
 
 

} // end of line"

SO - I've set up a for loop and am trying to use the variable 'i' that I use to advance to loop to also advance the array (Map[i]) so it will step through each tile name and can then access the Fog_Tile script on it. so... what am I doing wrong here?

I'm thinking PERHAPS I want to revers my logic and use my EndTurn script to set a flag and have each individual Fog_Tile script check for that flag; and I think I could pull that off, but want to understand where I am going wrong with for loops and arrays in case I want to use that in the future.

Comment
Add comment · Show 1
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 Fattie · Feb 07, 2013 at 10:32 AM 0
Share

as you can guess this is incredibly messy! :)

(1) generally don't use arrays, use List. there is zero performance issue at hand here and it is incredibly, staggeringly, clearer and easier to use

(2) you need a pool I'd say, or some form of List handling of your objects

http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html

(3) you need to get more with OO program$$anonymous$$g. almost certainly here you should have .. some sort of .. object that does something. those, err, things would do things on their own, and all this code would totally disappear

(4) if you explain what you are trying to do VERY SI$$anonymous$$PLY in terms of the game, someone will tell you how!

0 Replies

· Add your reply
  • Sort: 

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

10 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

Related Questions

Multiple Cars not working 1 Answer

Push down GUI everytime for loop cycles through Array 1 Answer

Values in Array Automatically Revert 1 Answer

Distribute terrain in zones 3 Answers

Affect every object in 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