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 Brutalitywarlord · May 18, 2016 at 02:10 PM · c#sortingmultidimensional array

Sorting an Array of Arrays for game Object

The purpose of the code is to check the spawn point of the unit being spawned, and if there is a game object there, to not spawn an object, but I have hit massive amounts of hurdles in what should have been a simple task. The new error here is that my multidimensional array is being temperamental

I have used Nested for-loops in an attempt to sort through a 2D array, but they keep giving me an error saying a nested loop initializer was expected, thing is when I switch out "[,]" for "[][]", I can no longer use the transform.position methods, so i need that "[,]" initializer, my code is included below, i will be most gratefully if you can repair this error and finally put an end to a code I've worked on for much to long

using UnityEngine; using System.Collections; using UnityEngine.UI; using Random = UnityEngine.Random;

public class Mspawn : MonoBehaviour { public Button button; public BoardManager boardManger;

 public Vector3 Coordinate;
 
 void OnEnable(){
     
     if (Input.GetMouseButtonDown(0)) {
         button.onClick.AddListener (SpawnUnit);
     }
 }
 
 void Awake(){
     boardManger = GetComponent<BoardManager> ();
     
 }
 void SpawnUnit()
 {
     Coordinate = randomSpawnPosition ();
     for (int i = 0; i < boardManger.PlayerUnits.Length; i++) 
     {
         
         if (boardManger.PlayerUnits[i].CompareTag("PlayerSoldierMarksman"))
         {
             if (checkposition(Coordinate) == false)
             {
                 Instantiate (boardManger.PlayerUnits[i], new Vector3 (Coordinate.x,Coordinate.y,0f), Quaternion.identity);
                 
             }
             Debug.Log("Selected Spawn Position "+ Coordinate);
         }
     }
 }
 public bool checkposition(Vector3 SpawnedPosition)
 {
    // This is the problem code, it returns a "Expected Nested array Initializer was expected" error
     GameObject[] AllMountains = GameObject.FindGameObjectsWithTag("Mountain");
     GameObject[] AllPlayerHQ = GameObject.FindGameObjectsWithTag ("PlayerHQ");
     GameObject[] AllMarksman = GameObject.FindGameObjectsWithTag ("PlayerSoldierMarksman");
     GameObject[,] AllActive = {AllMarksman, AllMountains, AllPlayerHQ};
     int maxRow = AllActive.GetLength(0);
     int maxCol = AllActive.GetLength (1);
     for (int i =0; i <= maxRow; i++) 
     {
         for (int x = 0; x<= maxCol; x++)
         {
             if (AllActive[i,x].transform.position == SpawnedPosition)
             {
                 Debug.Log("Spawn Blocked");
                 return true;
             }
         }
     }
     return false;
 }
 Vector3 randomSpawnPosition()
 {
     int x = Random.Range(0,2);
     int y = Random.Range(0,2);
     Vector3 Position = new Vector3 (x, y,0f);
     return Position;
     
 }

}

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

2 Replies

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

Answer by M-Hanssen · May 18, 2016 at 02:44 PM

Change your method to the following:

 public bool checkposition(Vector3 SpawnedPosition)
     {
         List<List<GameObject>> allActive = new List<List<GameObject>>();
         allActive.Add(GameObject.FindGameObjectsWithTag("Mountain").ToList());
         allActive.Add(GameObject.FindGameObjectsWithTag("PlayerHQ").ToList());
         allActive.Add(GameObject.FindGameObjectsWithTag("PlayerSoldierMarksman").ToList());
 
         if (allActive.Any(activeItem => activeItem.Any(activeGameObject => activeGameObject.transform.position == SpawnedPosition)))
         {
             Debug.Log("Spawn Blocked");
             return true;
         }
         return false;
     }

Make sure to add using System.Linq;

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 Brutalitywarlord · May 19, 2016 at 02:38 PM 0
Share

This code does not seem to function for some reason, it give a "List 1 could not be found, are you missing a directory ect....." error, and the code it'self is red, which from my experience indicates that i am unable to use it

avatar image M-Hanssen Brutalitywarlord · May 19, 2016 at 02:43 PM 0
Share

List is a Class inside the namespace System.Collections.Generic.

$$anonymous$$ake sure to add using System.Linq; and using System.Collections.Generic;on top of your script!!!

You can make use of Resharper for Visual Studio to find the missing references for you.

avatar image Brutalitywarlord M-Hanssen · May 19, 2016 at 02:52 PM 0
Share

This actually works now, thank you, and it also gets rid of that Array out of index problem i was having with the arrays, so you have done way more then asked, thankyou sir ^_^ have a wonderful evening

avatar image
0

Answer by Brutalitywarlord · May 19, 2016 at 02:39 PM

I have found the error, instead of using [i,x] i switch it to [i][x] instead

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

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

How do i sort multidimensional javascript array? 1 Answer

How to sort list in Leaderboard with animation 0 Answers

Array.Sort() - JS only???! 0 Answers

Getting objects out of a list and then comparing them 0 Answers

2d array isn't declared even though I declared it 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