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
2
Question by TheKnight · Nov 25, 2011 at 09:16 PM · c#arraysvaluesinitialization

C# array initialization

Hi to all.

I have a quick problem which I need to be solved.

Lately been looking into lots of C# tutorials, which helped a lot, but still there are some exceptions when using C# into Unity and regular using.

I initialize an array, which is called results, and set 5 elements to it. Now, what I want to do is initialize them in the script, and one more thing - if that is possible, they can hold a value of any int variable right?

Here's the code

 using UnityEngine;
 using System.Collections;
 
 public class Label : MonoBehaviour {
     int lives = 5;
     public int[] results = new int[] {15, 1645, 135, 567};
     // Use this for initialization
     void Start () {
         Debug.Log(results[1]);
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 }

It works fine if I initialize the values inside the editor, but this time it's not what I'm looking for. Thanks.

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

3 Replies

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

Answer by jahroy · Nov 26, 2011 at 02:37 AM

If you declare a variable as public, it can be edited in the Inspector.

Once the script has been attached to a GameObject, the init values in the script are ignored.

If those values were used, there would be no point of having the Inspector access.

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 ChefZweegie · Nov 26, 2011 at 02:54 AM 0
Share

Good point. If you want to init this only in the script, drop the public off of it.

avatar image TheKnight · Nov 26, 2011 at 11:42 AM 0
Share

Yes, this is what solved my problem. Thanks!

avatar image
4

Answer by ptdnet · Nov 26, 2011 at 02:21 AM

 public int[] results;

 void Update() {
     results = new int[5];
     results[0] = 324;
     // etc...
 }

Is that what you meant?

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 TheKnight · Nov 26, 2011 at 12:02 PM 0
Share

This is another way to do it, yes, but my mistake was with public. Thanks anyway!

avatar image VamshiKrishnaP · Nov 28, 2014 at 10:45 AM 0
Share

Hi Everyone!

I have "y" number of arrays and each array with "x" number of elements.

How can i create such array in runtime ?

can anyone suggest ?

"y" and "x" are the values co$$anonymous$$g from server. (i want to store Textures in arrays)

avatar image
-4

Answer by Projectxx858xxGames · Oct 25, 2016 at 08:17 PM

@VamshiKrishnaP You need to use Vector2[]

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 Bunny83 · Oct 25, 2016 at 10:10 PM 0
Share

The question is 5 years old and has already an accepted answer which addresses the actual problem correctly. So you bumped the question for no reason.

Apart from that your answer is just wrong in this case. He uses an int array. There's no point in using a Vector2 array.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Initialising List array for use in a custom Editor 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Get ParticleSystem (Shuriken) to play from array of game objects C# 2 Answers

Obtaining the Variable of a Prefab in an 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