Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by KBCreater · Jun 21, 2017 at 01:07 AM · grid based gamearray list

Is there a way to store an array in one script and access it in another?

I want to store an array of tile locations in one script where I can save the data and then have another script where it can decode the quads name and match it to find the array.

So I have a set of arrays named under a float land. Also in the process of generating the map I named the cloned quads (tiles) equal to the set of arrays. Thus allowing me to store data in an array and access it in each of the individual tile scripts. I am having a hard time finding bits of scripts that will work with each other. The way it should work in the other script is that it should get the tiles names and try to find its match in the array.

 using UnityEngine;
 using System.Collections;
 
 public class mapGenerator : MonoBehaviour {
 
     public Transform tilePrefab;
     public Vector2 mapSize;
     public float[][] land;
 
     [Range(0,1)]
     public float outlinePercent;
     void Start(){
         genMap ();
     }
     void Awake(){
         // initializes the first part of the array according to the mapsize.x
         land = new float[(int)100][];
         // loops through to initialize to represent the y 
         int i = land.Length;
         while (i > 0) {
             i--;
             land [i] = new float[(int)100];
         }
     }
     public void genMap(){
 
         string holderName = "Generated map";
         string tileId;
         float calculation;
         //allows the script to be edited and saved during play
         if (transform.FindChild (holderName)) {
             DestroyImmediate (transform.FindChild (holderName).gameObject);
         }
         //makes a "folder" for the tiles
         Transform mapHolder = new GameObject (holderName).transform;
         mapHolder.parent = transform;
         //map generator. Every for each row It fills the row with tiles
         for (int x = 0; x < mapSize.x; x++) {
             for (int y = 0; y < mapSize.y; y++) {
                 //makes and moves a single new tile
                 Vector3 tilePos = new Vector3 (-mapSize.x / 2 + 0.5f + x, 0, -mapSize.y/2 + 0.5f + y);
                 Transform newTile = Instantiate (tilePrefab, tilePos, Quaternion.Euler(Vector3.right*90)) as Transform;
                 //makes an outline
                 newTile.localScale = Vector3.one * (1 - outlinePercent);
                 //puts it in the file
                 newTile.parent = mapHolder;
                 //names the tile based on its chordinate
                 tileId = "" + ((x*mapSize.y)+y) + "";
                 newTile.name = tileId;
             }
         }
 
     }
 }
 


By the way there are 10,000 tiles so this is kinda necessary to store the data.

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 NoseKills · Jun 21, 2017 at 06:14 AM 0
Share

What exactly is the question? How to access "land" in other scripts?

avatar image KBCreater NoseKills · Jun 28, 2017 at 01:44 AM 0
Share

Yes. I need to access the array in another script.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Major · Jun 28, 2017 at 03:51 AM

Use GetComponent like you would for any other value, or set a variable of type mapGenerator is whatever script you need to access the values from, and set it to the instance you need (a gameobject with this script applied).

Comment
Add comment · 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

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

108 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 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 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 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 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

Move object to grid 1 Answer

How to make certain tiles a certain state 1 Answer

How can I set the exact size of an object in pixels? 1 Answer

What is the easiest way to enable 2D isometric grid-based pathfinding? 2 Answers

moving in a grid (2D) 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