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 Supershandy · Nov 12, 2013 at 08:54 PM · arrayvariableforiterate

Getting information from an array

Hello everyone, I have a bit of a problem on my hands.

I have created a grid of points using a 2d array and that part works fine, now for each point I have a script with an int variable and I want that variable filled with information from another array for each point.

part of the code:

 #pragma strict
 
 var starPoint : GameObject;
 var noOfStars : int;
 
 var milkyWay : int[] = [0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  5, 10, 11, 19, 17,  9,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 10, 13, 14, 25, 24, 12,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  8, 12, 13, 12,  8,  0,  0, 11, 14, 15, 24, 23,  9,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  8, 14, 16, 17, 16, 14,  8,  0, 16, 25, 27, 25, 16,  0,  0,  6,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0, 10, 14, 15, 14, 10,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  5, 22, 27, 28, 24, 18, 16, 12,  0, 17, 25, 38, 37, 26,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0];
 
 var width : int = 25;
 var length : int = 25;
 var gridPoint : GameObject [,];
 
 function Awake () 
 
 {
     gridPoint = new GameObject[width, length];
     
     for (var x : int = 0; x < width; x++)
     {
         for (var z : int = 0; z < length; z++)
         {
             var gridPointCentre : GameObject;
             
             if (milkyWay [z * 20 + x] >= 0)
             {
                 gridPointCentre = starPoint;
             }
             gridPoint[x, z] = Instantiate (gridPointCentre, Vector3(x * 100, 0, z * 100), Quaternion.identity);
         }
     }
 }

Now the value I want to fill the int variable with in the other script for each object is from the milkyWay array so that every object has a value i.e object 405 : starNumber = 5, object 406 : starNumber = 22, object 407 : starNumber = 0 etc etc, I tried to iterate through the array using a for loop but weirdly every value came back as "2" or "9".

Now what I want to happen is for that value to then be divided by 4 to give a final value that can then be used for instantiation.

Problem is, how do I do this?

Many thanks

Comment
Add comment · Show 2
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 Essential · Nov 12, 2013 at 09:46 PM 0
Share

I'm not fully sure what you're trying to do... "for each point I have a script with an int variable and I want that variable filled with information from another array for each point". – Does that mean you have a script for every point in the array or you have two arrays or...? Try and clarify what you're trying to do.

avatar image Supershandy · Nov 13, 2013 at 04:05 AM 0
Share

Sorry, i'll try to be more clear.

Basically i have two scripts, one to generate a grid of empty gameobjects, the other script is attached to the gameobject prefab that deals with creating objects from a number.

Now the grid i have set up, however what i now what to do is take the information from the milkyWay Array and place that information in the empty gameobjects or gridpoints, there are over 16,000 values in the array and 16,000 empty game objects, each gameobject has a variable in a script that i want filled with a number from the array.

Problem is, i got stuck while doing it, I tried a for loop to add one number to one one gameobject but i got some strange results.

1 Reply

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

Answer by Essential · Nov 14, 2013 at 09:59 PM

 var starPrefab : GameObject;
 
 private var width = 128;
 private var height = 128;
 
 function Awake ()
 {
     for ( var w=0; w < width; w++ )
     {
         for ( var h=0; h < height; h++ )
         {
             var go = Instantiate ( starPrefab, Vector3(w*10, h*10, 0), Quaternion.identity );
             go.GetComponent(StarScript).brightnessValue = milkyway[(w*height)+h];
         }
     }
 }

Keep in mind this requires a milkyway array that contains 128*128 (16384) values in it or it will give an error.

I suggest that once you've Instantiated this set of gameobjects you save it to the scene, so it doesn't need to get Instantiated every time it's run. For that you would move the generation code from Awake() into an editor script. How to do this can be found on Google.

Comment
Add comment · Show 8 · 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 Supershandy · Nov 15, 2013 at 04:04 AM 0
Share

The code is really that short? Blimey!

I'll give it a try later tonight as i'm getting sorted out for work but i'm pretty sure getting that amount of values won't be a problwm, will probably just be a case of adding a few extras 0's.

avatar image Essential · Nov 15, 2013 at 04:39 AM 0
Share

What you want is pretty simple. Study the code and follow what it's doing…

The width loop goes along sideways once then the height loop begins. Each step of the height loop goes down one and then instantiates a star at that point, it also gets that new star's script (I assume you've given the star prefab a script called StarScript or something similar), and then sets the brightnessValue var within the StarScript. Then the height loop steps down one and creates another star in the same manner. When the height loop reaches the bottom, the width loop take another step sideways and the height loop starts from the top again.

But if I were you, I'd start with just a few stars (8 width x 8 height = 64 stars). Once that works you can consider going to 16,000.

avatar image Supershandy · Nov 15, 2013 at 04:44 AM 0
Share

Haha, sorry, i should have said that i'd read the code, i just didn't know how to retrieve information from an array and put it into another script.

But yeah, i was planning on starting small as i'm not sure performance wise how it's going to handle it.

Thank you though for the help.

avatar image Supershandy · Nov 15, 2013 at 07:41 PM 0
Share

Just tried the code with a 30x30 grid and the 900 starPrefab's (as you called it) are created fine, however the brightnessValue (as you called it) remains at zero for each one?

Could it be a problem with the milkyWay array I have it set as var milkyWay : int[] = [all the numbers];

avatar image Essential · Nov 15, 2013 at 09:23 PM 0
Share

Hmm... try adding 'private' in front of the milkyWay var.

In fact, it's good practice to have all variables as private unless you need them to be accessible from elsewhere.

Show more comments

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

18 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

Related Questions

How to make an array of Interactive Clothes in one variable 0 Answers

Make an array with different variables... 3 Answers

deleting a face of a proceduraly created mesh. 1 Answer

changing variable based on name of objects in array. 1 Answer

A node in a childnode? 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