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 IMTRIGGERHAPPY9 · Oct 23, 2011 at 05:16 AM · instantiatearraygetcomponentmultidimensional

passing multi-dimensional arrays from one script to another?

ok so i have a map layout: 125*JS GridMaker*125 var instantiateObject : GameObject;

 var gridX = 10;
 var gridY = 10;
 
 
 function Start () {
 
 var map : GameObject[,] = new GameObject[gridX,gridY];
 
 
     for ( var x = 0; x < gridX; x++){
         
         for (var y = 0; y < gridY; y++){
         
             var myName = Instantiate(instantiateObject, Vector3(x *.5,0,y * .5), Quaternion.identity);
                 
             myName.name = "x: " + x + "y: " + y;
             
         map[x,y] = myName;
             
         //Debug.Log(map[x,y]);
         }
         
      } 
 
             
         
 }

and i have a instantiate where i am trying to acces that maps gameObjects: JS GuiStart

 // assign my grid point
 function Start () {
        middleOfMap = GetComponent(GridMaker);
        leftMiddle = middleOfMap.Map[1,1];
        }
 
 function OnGui () {
            //If gui button is down Instantiate monster one at gridpoint
            if(GUI.Button (Rect (Screen.width - (Screen.width - 10) + monsterOneRight,Screen.height - 65,50,50), "Monster_1")){
        Instantiate(monster1,leftMiddle,Quaternion.identity);
        }    


but it gets confused about which gameObject i am trying to acces in my array?

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
1
Best Answer

Answer by aldonaletto · Oct 23, 2011 at 06:04 AM

You should move the map declaration outside Start - variables declared inside a function are local, and nobody outside knows them.

var gridX = 10; var gridY = 10; var map : GameObject[,] = new GameObject[gridX,gridY];

function Start () { ... In the GuiStart, you must define the type of middleOfMap as GridMaker. If both scripts are attached to different objects, you must have a reference (gameObject, collider, transform, etc.) to the GridMaker owner. You may also have problems because you're reading the map value at Start, but nobody can guarantee that GridMaker's Start function will be executed prior to GuiStart - read the map value in OnGUI instead:

var middleOfMap: GridMaker;

function Start () { middleOfMap = GetComponent(GridMaker); // both scripts must be attached to the same object! }

function OnGui () { //If gui button is down Instantiate monster one at gridpoint if (GUI.Button (Rect (Screen.width - (Screen.width - 10) + monsterOneRight,Screen.height - 65,50,50), "Monster_1")){ var leftMiddle = middleOfMap.map[1, 1].transform.position; // read the map value here Instantiate(monster1,leftMiddle,Quaternion.identity); }
}

Comment
Add comment · Show 6 · 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 IMTRIGGERHAPPY9 · Oct 23, 2011 at 05:25 PM 0
Share

now its giving me this error:

No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.GameObject, UnityEngine.Transform, UnityEngine.Quaternion)' was found

i dont get it cause i have been using this exact same formula through out my entire game...

avatar image IMTRIGGERHAPPY9 · Oct 23, 2011 at 05:43 PM 0
Share

ok so i found my real problem, i did a debug.log of my left$$anonymous$$iddle var and it says it cant find my array. well this is actually what it says

Field 'Grid$$anonymous$$aker.map' not found.

avatar image aldonaletto · Oct 23, 2011 at 08:34 PM 0
Share

These scripts - Grid$$anonymous$$aker and GuiStart - are attached to the same object? If not, you must have a reference to the Grid$$anonymous$$aker object in GuiStart - kind of:

 var gridObject: Transform;

And drag the object to which Grid$$anonymous$$aker is attached to this variable. Then you must change the first line of Start to :

 middleOf$$anonymous$$ap = gridObject.GetComponent(Grid$$anonymous$$aker);
avatar image IMTRIGGERHAPPY9 · Oct 23, 2011 at 08:48 PM 0
Share

no they are on the exact same object named controller.

avatar image aldonaletto · Oct 23, 2011 at 09:26 PM 0
Share

Have you used exactly the script GuiStart above? Because Unity is complaining that a Transform is being passed as the second argument of Instantiate, but the line below:

 var left$$anonymous$$iddle = middleOf$$anonymous$$ap.map[1, 1].transform.position; 

forces left$$anonymous$$iddle to be a Vector3, since it's receiving middleOf$$anonymous$$ap.map[1,1].transform.position. You can define left$$anonymous$$iddle as a Vector3:

 var left$$anonymous$$iddle: Vector3 = middleOf$$anonymous$$ap.map[1, 1].transform.position; 

You must also check if Grid$$anonymous$$aker is correct:

var gridX = 10; var gridY = 10; // this line must be here, not inside Start! var map : GameObject[,] = new GameObject[gridX,gridY];

function Start () { for ( var x = 0; x < gridX; x++){ for (var y = 0; y < gridY; y++){ var myName = Instantiate(instantiateObject, Vector3(x .5,0,y .5), Quaternion.identity); myName.name = "x: " + x + "y: " + y; map[x,y] = myName; //Debug.Log(map[x,y]); } } }

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to make an Array equal the Components of an Instantiated GameObject 1 Answer

Get unity to recognize prefab in C# 2 Answers

IndexOutOfRangeException: Array index is out of range when using an Array and instantiating 2 Answers

How to Instantiate a Different Game Object After Getting to the End of an Array? 1 Answer

Instantiate an array of prefabs? 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