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 Remm · Apr 23, 2013 at 10:41 PM · c#prefabarray

Array only returning first value C#

     public Unit[] getUnits()
     {
         return units;    
     }

Basically this thing is being stubborn, the reasoning is beyond me. It has 9 values in it that can be accessed with other functions such as

     public Unit unit(int num)
     {
         return units[num];    
     }

however attempting to return the array for a foreach is met with errors when attempting to manipulate past the first(0) object. The below code will allow the unit to do the animation -- but only the first one.

 foreach (Unit u in players[0].army.getUnits())
         {
             u.anim(ACTION.STRIKE3);
         }

It feels like something dumb is being done by me, but I can't for the life of me figure out what it is. The unit Army class is being accessed in the first two functions, trying to get the unit class objects, which calls prefabs.

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
0

Answer by EliteMossy · Apr 23, 2013 at 10:59 PM

public Unit[] getUnits() { return units; }

is not really the proper way to do it. You should use properties for something simple like this.

 private Unit[] units;
 
 public Unit[] Units{ get { return units; } }

Is a proper way to do it, then

 foreach (Unit u in players[0].army.Units)
        {
          u.anim(ACTION.STRIKE3);
        }

I am just assuming this is what you are after. Also check to see what the length of Units is, by doing Debug.Log(players[0].army.Units.Length);

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 Remm · Apr 23, 2013 at 11:01 PM 0
Share

The length is 9, as it should be, but get nullRef on second pass... will try this and see if there are any changes.

avatar image Remm · Apr 23, 2013 at 11:09 PM 0
Share

You were right about my lacking syntax. I've been doing web development for far too long it seems.

However the problem still persists. Only thing I could think is that the problem lies in the constructor or in the Unit class.

 public Army(string pregen)
     {
         if (pregen == "test")
         {
             //9 units
             units = new Unit[9];
             units[0] = new Unit(JOB.SOLDIER);
             units[2] = new Unit(JOB.SOLDIER);
             units[3] = new Unit(JOB.SOLDIER);
             units[4] = new Unit(JOB.ARCHER);
             units[5] = new Unit(JOB.ARCHER);
             units[6] = new Unit(JOB.THEIF);
             units[7] = new Unit(JOB.THEIF);
             units[8] = new Unit(JOB.$$anonymous$$AGE);
             
         }
     }
avatar image Remm · Apr 23, 2013 at 11:13 PM 0
Share

WAIT. I'$$anonymous$$ AN IDIOT. I SEE IT NOW

avatar image whydoidoit · Apr 23, 2013 at 11:20 PM 0
Share

:) I was about to point that out...

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

13 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

Related Questions

Distribute terrain in zones 3 Answers

How to load prefabs into array 2 Answers

PrefabHell - How to have scripts of prefabs instances reference the variables of the prefab instance they're attached to? 1 Answer

How to prevent prefab of custom editor inspector from overriding array size. 1 Answer

How to deal with for loop and array? 0 Answers


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