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 Robomaster · Jan 11, 2014 at 06:32 PM · gameobjectarraysvariablessaving

Is it possible to store variables in arrays?

I am currently trying to figure out how to store 5 variables in a array. I have my arrays size as 4 but its not letting me put variables in. I have my games so that the user picks 5 heroes and then that script saves that heroes data/gameobject to the variable. These are 2 different scripts as well so is it possible to save data to a array that's in another script? I was thinking something like:

using UnityEngine;

using System.Collections;

public class HeroArraySaver : MonoBehaviour {

GameObject heroOne;

GameObject heroTwo;

GameObject heroThree;

GameObject heroFour;

GameObject heroFive;

// Use this for initialization void Start () {

}

// Update is called once per frame void Update () {

GameObject[] array = new GameObject[4];

 array[0] = heroOne;

 array[1] = heroTwo; 

 array[2] = heroThree;

 array[3] = heroFour;

 array[4] = heroFive;

} }

The reason i chose GameObject is because a GameObject will be saved to these variables.

Comment
Add comment · Show 9
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 tanoshimi · Jan 11, 2014 at 06:42 PM 0
Share

It's certainly possible to store an array of gameobjects, but if you want to store 5 of them you should declare an array of size 5, not 4:

 GameObject[] array = new GameObject[5];

"Not letting me put variables in", "Not working"... why? What error message?

avatar image agies1 · Jan 11, 2014 at 07:41 PM 0
Share

What do you mean by doesn't work? Is it throwing an exception? Can you provide the entire code block that isn't working?

avatar image helloiam · Jan 11, 2014 at 07:47 PM 0
Share

Are heroOne etc gameObjects?

avatar image helloiam · Jan 11, 2014 at 07:49 PM 0
Share

And make sure there are private if you want to acces the gameObject by script otherwise you will make them public but you have to fill the whole array you need to have 5 gameObjects in it.

avatar image Robomaster · Jan 11, 2014 at 08:39 PM 0
Share

What I mean by not letting me put the variables in I mean when I type in the code array[0] = heroOne; it says it can't find that variable, I even tried declaring it and it said the same thing. Also the reason I put 4 is because arrays start at 0 right? And yes heroOne is a GameObject, the player choses his 5 heroes and they get saved to heroOne, heroTwo, etc. Also when I get back to my computer ill edit it with my full code. Thanks

Show more comments

1 Reply

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

Answer by agies1 · Jan 11, 2014 at 10:12 PM

Your code is valid C# code, however the Length of the Array is invalid. While C# arrays are 0 based index, when you initialize an Array you are supplying the Array size as the parameter. When I use the below code everything is valid in a new test game.

 void Update()
     {
         GameObject[] array = new GameObject[5];
         array[0] = heroOne;
         array[1] = heroTwo;
         array[2] = heroThree;
         array[3] = heroFour;
         array[4] = heroFive;
 
     }

One observation is that you are creating a new Array on each frame, you should be careful about this, since the garbage collector will have alot of work ahead of it.

Comment
Add comment · Show 3 · 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 Robomaster · Jan 12, 2014 at 02:31 AM 0
Share

Okay thanks i just adjusted my code to match yours and now it working, i don't know why it wasn't working before since it was valid but thanks for the help and helping me understand Arrays even better!

avatar image landon912 · Jan 12, 2014 at 02:49 AM 0
Share

You had '4' ins$$anonymous$$d of '5'. Arrays are 0 based but there are still five GameObjects so you must supply the total number as if it was 1-based.

avatar image Robomaster · Jan 12, 2014 at 03:15 AM 0
Share

Got it thank you! Thanks for the help!

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

22 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

Related Questions

Select every GameObject in GameObject Array 2 Answers

Editing properties of game objects inside an array using the inspector. 2 Answers

Filling up an array with GameObjects 0 Answers

Showing GUI by pressing diffrent buttons? 1 Answer

odd error in c# script when working with arrays 2 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