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 /
This question was closed May 04, 2016 at 01:58 PM by Brutalitywarlord for the following reason:

A solution was found

avatar image
0
Question by Brutalitywarlord · May 02, 2016 at 02:56 PM · c#2dspritespawningrandomspawning

Random Layout of tiles notworking in 2d game

Below is the code for a game i'm making for a school project, i have some issues with laying out a random amount of a blocking object which i named "Mountain", most of the code is taken from the 2D rogue-like so im confused as to what the problem is, below is my code with comments highlighting the problem areas, and for the records the Set UP Scene function is being called with the proper parameters, it works there should be nothing wrong with that as it spawns the players bases and the actual game board itself, it's randomly spawning the mountains(Similar to the walls in the rogue-like) at random positions that is dis-functional

Here is my code

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

public class BoardManager : MonoBehaviour {

 [Serializable]
 public class Count
 {
     public int maximum;
     public int minimum;
     public Count(int min, int max)
     {
         int maximum = max ;
         int minimum = min;
     }
 }

 public int actions;
 public int Health;
 public int Range;
 public int Movement;
 public int SpawnCost;
 public int Damage;    
 public GameObject PlayerHQ;
 public GameObject EnemyHQ;

 public GameObject[] Mountain;
 public GameObject[] Floortiles;
 public GameObject[] PlayerUnits;
 public GameObject[] EnemyUnits;
 public int rows = Random.Range(50,500);
 public int columns = Random.Range(50,500);

 private Transform BoardHolder;
 private List<Vector3> GridPositions = new List<Vector3>();

 void CreateBoard()
 {
     GridPositions.Clear ();
     for (int x = 1; x < columns - 1; x++) 
     {
         for (int y = 1; y < rows - 1;y++)
         {
             GridPositions.Add(new Vector3 (x,y,0f));
         }
     }


 }
 void setUpBoard()
 {
     BoardHolder = new GameObject ("Board").transform;
     for (int x = - 1; x < columns + 1; x++) 
     {
         for (int y = -1; y < rows + 1 ;y++)
         {
             GameObject toInsantiate = Floortiles[Random.Range(0,Floortiles.Length)];
             if (x == -1 || x == columns || y == -1 || y == rows)
             {
                 toInsantiate = Mountain[Random.Range(0,Mountain.Length)];
                 
             }
             GameObject instance = Instantiate(toInsantiate, new Vector3(x,y,0f), Quaternion.identity) as GameObject;
             instance.transform.SetParent(BoardHolder);
         }
     }
     Instantiate(PlayerHQ, new Vector3 (columns - columns,rows - rows , 0f), Quaternion.identity);
     Instantiate(EnemyHQ, new Vector3(columns - 1 , rows - 1,0f), Quaternion.identity);

 }
 Vector3 RandomPosition()
 {
     int randomIndex = Random.Range (0, GridPositions.Count);
     Vector3 randomPosition = GridPositions [randomIndex];
     GridPositions.RemoveAt (randomIndex);
     return randomPosition;
 }
 public GameObject LayOutRandom(GameObject[] tileArray,int minimum,int maximum)
 {
     int objectCount = Random.Range (minimum, maximum);
     for (int i = 0; i < objectCount; i++) 
     {
         Vector3 randomPosition = RandomPosition();
         GameObject tileChoice = tileArray[Random.Range(0,tileArray.Length)];
         return tileChoice;
     }
 }
 public void setupScene(int level)
 {

     setUpBoard ();
     CreateBoard ();
             //This is the problem code, this function does not spawn objects, and it's not a layering issue as the game objects themselves are not being spawned
     LayOutRandom(Mountain,10,25);

 }

}

Comment
Add comment · Show 1
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 Cherno · May 02, 2016 at 03:50 PM 0
Share
  1. Please format your code properly.

  2. Insert Debug.Log lines to see which parts of your code are being called, and to print specific values to the console.

1 Reply

  • Sort: 
avatar image
0

Answer by NoseKills · May 02, 2016 at 05:08 PM

The method LayOutRandom() does not instantiate anything. It just returns a random prefab from the array 'Mountain' (which you pass into it). It also pulls a random position with RandomPosition() and then doesn't do anything with it. You should call Instantiate to spawn the mountain and set its position to whatever RandomPosition() returns. And the method shouldn't return a value unless you intend to do something with it, at least not before running the loop to its end.

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 Brutalitywarlord · May 03, 2016 at 04:39 AM 0
Share

thank you for the answer, i'll see if this works tomorrow, if not i'll reply stating so

avatar image Brutalitywarlord Brutalitywarlord · May 03, 2016 at 02:24 PM 0
Share

I have added the instatiate command to the function and it is still dis-functional

this is the new code below

void LayOutRandom(GameObject[] tileArray,int $$anonymous$$imum,int maximum) { int objectCount = Random.Range ($$anonymous$$imum, maximum); for (int i = 0; i < objectCount; i++) { Vector3 randomPosition = RandomPosition(); GameObject tileChoice = tileArray[Random.Range(0,tileArray.Length)]; Instantiate (tileChoice, randomPosition , Quaternion.identity);

     }
 }

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Two objects sharing a script should spawn different random objects, but instead they spawn the same random object. 1 Answer

How to make sprite follow gameObject on trigger? 0 Answers

Player rotation 2d 0 Answers

Silent failure of Sprite.Create() ? 0 Answers

Destroying 2D terrain at runtime 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