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

Solution to one problem found,

avatar image
0
Question by Brutalitywarlord · Apr 29, 2016 at 07:38 PM · c#2dspawnrandomization

Objects will not spawn in a 2d game.

I am making a 2d game for my class, admit-ably i am using mostly code from the rogue-like to accomplish this but i am using original sprites, however some of my functions are currently dis-functional, i have basically copied code from the rogue like and edited it to fit my variables, but the objects i wish to spawn will not spawn, this game is supposed to be a TBS style game, and the bases will not spawn,

this is an issue with spawning and not layers btw, i have checked the inspector during play mode, the objects i desire to be created are not even being initialized

below is a copy of my code, the problem areas will be highlighted with comments

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 int mountains = Random.Range(100,5000);
 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);
         }
     }
 }
 Vector3 RandomPosition()
 {
     int randomIndex = Random.Range (0, GridPositions.Count);
     Vector3 randomPosition = GridPositions [randomIndex];
     GridPositions.RemoveAt (randomIndex);
     return randomPosition;
 }
 void LayOutRandom(GameObject[] tileArray, int minimum, int maximum)
 {
     int objectCount = Random.Range (minimum, maximum + 1);
     for (int i = 0; i < objectCount; i++) 
     {
         Vector3 randomPosition = RandomPosition();
         GameObject tileChoice = tileArray[Random.Range(0,tileArray.Length)];
         Instantiate (tileChoice, randomPosition , Quaternion.identity);
         
     }
 }
 public void setupScene(int level)
 {
     setUpBoard ();
     CreateBoard ();
             // Similar to the function in the Rogue-like where it randomly spawns walls throughout the map, this function is supposed to do the same, but doesn't even initialize the object.
     LayOutRandom(Mountain,columns, rows);
             //The player and Enemy base will not spawn no matter what I try
     Instantiate(PlayerHQ, new Vector3 (columns - (columns -1),rows - (rows -1), 0f), Quaternion.identity);
     Instantiate(EnemyHQ, new Vector3(columns - 1 , rows - 1, 0f), Quaternion.identity);
 }

}

Comment
Add comment · Show 7
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 rodude123 · Apr 29, 2016 at 07:44 PM 1
Share

You haven't put setupScene(int level) in the update function. If you have then I don't know what to do

avatar image iHaveReturnd · Apr 29, 2016 at 08:08 PM 1
Share

Where is setupScene being called? Have you made sure that function is running?

Show more comments
avatar image iHaveReturnd · May 02, 2016 at 04:45 PM 0
Share

Are you getting any errors you haven't mentioned with CreateBoard/SetupBoard/LayOutRandom?

If the rest is working the only things I can think of are that the function isn't being called properly which you've said it is, or there's an error stopping the code from getting to your last instantiate calls.

As a quick test, you could just add your line 90 "Instantiate(PlayerHQ, new Vector3 (columns - (columns -1),rows - (rows -1), 0f), Quaternion.identity);" to another function that gets called. From what you've said it sounds like it should work.

0 Replies

  • Sort: 

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Player enable 1 Answer

How to make objects appear with mouse click in a row? 2 Answers

Sound effect issue 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