- Home /
Solution to one problem found,
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);
}
}
You haven't put setupScene(int level)
in the update function. If you have then I don't know what to do
Where is setupScene being called? Have you made sure that function is running?
i believe the setUpScene is being called in the Game$$anonymous$$anager script i have, the level parameter isn't included, but it still renders the basic board with the floor tiles in the middle and the mountains surrounding the outside, it's just that the random placement for the inner mountain tiles is not functional, and the player bases will not spawn, they are pretty much re-textured cloned "Exit Object" from the rogue-like, except they are on the blocking layer ins$$anonymous$$d
To make an easy check, it'd be good to add a debug log to your setupScene function, since right now it still sounds like it might not be getting called accurately.
If you are calling setupScene(); without a parameter, it would be calling a different setupScene function, since the one you have listed requires an int to be passed into it.
If setUpBoard is running it implies that this function is running, but I would add a debug just to be sure because like I mentioned earlier it sounds like you may actually be calling a different method if you did not pass in an int for the level id.
Otherwise if you're not getting any errors the code looks like it should work.
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.
Follow this Question
Related Questions
Player enable 1 Answer
How to make objects appear with mouse click in a row? 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
[UNET] Objects other than the local player's have a small jitter when moving. 1 Answer