Instantiate GameObjects over network - c#

I have a piece of code that is supposed to generate a random tile map which it does for the server only. I can't figure out how to get it to sync to the players when they join it.How would I sync the prefabs that are instigated to the joining players? They load on one player (the server) but when another player joins they don't show up.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class GenerateLevel : NetworkBehaviour {
public int MapSize = 20;
public GameObject prefab;
public GameObject _LeftW;
public GameObject _RightW;
public GameObject _TopW;
public GameObject _Botw;
public GameObject SpawnLocRed;
public GameObject SpawnLocBlue;
public string map;
public override void OnStartServer()
{
//base.OnStartServer();
GetComponent<Renderer>().material.color = Color.blue;
}
void Start()
{
if (!isServer)
{
return;
}
for (int c = MapSize / 2; c > -MapSize / 2; c--)
{
for (int r = -MapSize / 2; r < MapSize / 2; r++)
{
var t = Instantiate(prefab, new Vector3(r, 0, c),
Quaternion.identity);
NetworkServer.Spawn(t);
Debug.Log("Col:" + c + " Row:" + r);
if (Random.Range(0, 3) == 2)
{
t.GetComponent<RAISE>().Run();
map += 1;
}
else
{
map += 0;
if (c < 0 - MapSize / 4 && r > 0 + MapSize / 4)
{
var red= Instantiate(SpawnLocRed, new Vector3(r, 2, c),
Quaternion.identity);
}
if (c > 0 + MapSize / 4 && r < 0 - MapSize / 4)
{
var blue= Instantiate(SpawnLocBlue, new Vector3(r, 2, c),
Quaternion.identity);
}
}
map += "r";
}
map += "c";
}
Debug.Log(map);
if (MapSize % 2 == 0)
{
GameObject wt = Instantiate(_TopW, new Vector3(-.5f, 0, -MapSize / 2), Quaternion.identity);
wt.transform.localScale = new Vector3(MapSize, 5, 1);
GameObject wb = Instantiate(_Botw, new Vector3(-.5f, 0, MapSize / 2+1), Quaternion.identity);
wb.transform.localScale = new Vector3(MapSize, 5, 1);
GameObject wl = Instantiate(_LeftW, new Vector3(-MapSize / 2-1, 0, .5f), Quaternion.identity);
wl.transform.localScale = new Vector3(1, 5, MapSize+2);
GameObject wr = Instantiate(_RightW, new Vector3(MapSize / 2, 0, .5f), Quaternion.identity);
wr.transform.localScale = new Vector3(1, 5, MapSize+2);
}
else
{
GameObject wt = Instantiate(_TopW, new Vector3(-.5f, 0, -MapSize / 2), Quaternion.identity);
wt.transform.localScale = new Vector3(MapSize-1, 5, 1);
GameObject wb = Instantiate(_Botw, new Vector3(-.5f, 0, MapSize / 2 + 1), Quaternion.identity);
wb.transform.localScale = new Vector3(MapSize-1, 5, 1);
GameObject wl = Instantiate(_LeftW, new Vector3(-MapSize / 2 - 1, 0, .5f), Quaternion.identity);
wl.transform.localScale = new Vector3(1, 5, MapSize + 1);
GameObject wr = Instantiate(_RightW, new Vector3(MapSize / 2, 0, .5f), Quaternion.identity);
wr.transform.localScale = new Vector3(1, 5, MapSize + 1);
}
Debug.Log(MapSize % 2);
}
Here is the updated snippet code
public GameObject[] tiles = new GameObject[1000];
public string map;
public override void OnStartServer()
{
//base.OnStartServer();
GetComponent<Renderer>().material.color = Color.blue;
}
public void OnPlayerConnected(NetworkPlayer player)
{
foreach(GameObject go in tiles)
{
NetworkServer.Spawn(go);
}
}
void Start()
{
if (!isServer)
{
return;
}
int temp = 0;
for (int c = MapSize / 2; c > -MapSize / 2; c--)
{
for (int r = -MapSize / 2; r < MapSize / 2; r++)
{
var t = Instantiate(prefab, new Vector3(r, 0, c), Quaternion.identity);
tiles[temp] = t;
NetworkServer.Spawn(t);
}
}
}
After some fiddiling I got the tiles to spawn but they are not syncing ot the characters the henerate tile is running for each client seperatrly.

You can't just use the Instantiate function to make prefabs show on the network. There are other things you must do:
1.You must register the prefab through the NetworkManager component. If there are more than one prefab, you must do this for all of them:
2.Attach NetworkIdentity component to the prefab. Note that if you also want to sync the objects transform, you must attach NetworkTransform to the prefab too.
3.After instantiating the prefab, use NetworkServer.Spawn to also instantiate it on the network too.
For example:
//Instantiate on this device
GameObject red = Instantiate(SpawnLocRed, new Vector3(r, 2, c),
Quaternion.identity);
//Instantiate it on all clients
NetworkServer.Spawn(red);
Finally, you may want to wrap all the code around inside isLocalPlayer to make sure you only instantiate from the local player.
if (isLocalPlayer)
{
//Instantiate on this device
GameObject red = Instantiate(SpawnLocRed, new Vector3(r, 2, c),
Quaternion.identity);
//Instantiate it on all clients
NetworkServer.Spawn(red);
}

Related

cannot convert from UnityEngine.Vector3 to System.Collections.Generic.List<UnityEngine.Vector3>

I'm making a total war demo game where you drag on the screen to move your units. This bug has been bothering me and I don't know what's the reason behind it. The error I get is below.
FormationScript.cs(119,44): error CS1503: Argument 1: cannot convert from 'UnityEngine.Vector3' to 'System.Collections.Generic.List<UnityEngine.Vector3>'
Here is the code. (sorry for its length)
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Debug = UnityEngine.Debug;
public class FormationScript : MonoBehaviour
{
public GameObject[] units;
public Transform formationBarrier;
float unitWidth = 2.0f; // This also includes the gap between them, in this case the unit width is 1 and the gap is also 1
private float numberOfUnits;
private double unitsPerRow;
private float numberOfRows;
Vector3 startClick;
Vector3 endClick;
Vector3 selectedAreaSize;
Vector3 selectedAreaPos;
float startMinusEndX;
float startMinusEndZ;
private List<List<Vector3>> availablePositions;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Formation();
}
void Formation()
{
if (Input.GetMouseButtonDown(1))
{
Plane plane = new Plane(Vector3.up, 0);
float distance;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (plane.Raycast(ray, out distance))
{
startClick = ray.GetPoint(distance);
}
Debug.Log(startClick);
}
if (Input.GetMouseButtonUp(1))
{
Plane plane = new Plane(Vector3.up, 0);
float distance;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (plane.Raycast(ray, out distance))
{
endClick = ray.GetPoint(distance);
}
Debug.Log(endClick);
}
// ======================================================================================================
/*if (startClick.x - endClick.x < 0 || startClick.z - endClick.z < 0)
{
startMinusEndX = startClick.x + endClick.x;
startMinusEndZ = startClick.z + endClick.z;
}
else
{
startMinusEndX = startClick.x - endClick.x;
startMinusEndZ = startClick.z - endClick.z;
}
if (startMinusEndX > 0 && startMinusEndZ > 0)
{
formationBarrier.localScale = new Vector3(startMinusEndX, 1, startMinusEndZ);
formationBarrier.localPosition = new Vector3((startClick.x + endClick.z) / 2, 0, (startClick.z + endClick.z) / 2);
}
else if (startMinusEndX < 0 && startMinusEndZ > 0)
{
formationBarrier.localScale = new Vector3(startMinusEndX, 1, startMinusEndZ);
formationBarrier.localPosition = new Vector3((startClick.x + endClick.z) * 2, 0, (startClick.z + endClick.z) / 2);
}
*/
startMinusEndX = startClick.x - endClick.x;
startMinusEndZ = startClick.z - endClick.z;
formationBarrier.localScale = new Vector3(startMinusEndX, 1, startMinusEndZ);
formationBarrier.localPosition = new Vector3((startClick.x + endClick.z) / 2, 0, (startClick.z + endClick.z) / 2);
// ======================================================================================================
selectedAreaSize = formationBarrier.localScale;
selectedAreaPos = formationBarrier.localPosition;
numberOfUnits = units.Length;
unitsPerRow = (unitWidth / numberOfUnits) * selectedAreaSize.x;
unitsPerRow = Math.Round(unitsPerRow, 0);
if (unitsPerRow < 0)
{
unitsPerRow = unitsPerRow * -2;
}
if (numberOfRows % 1 == 0)
{
numberOfRows = numberOfUnits % (float)unitsPerRow;
for (int i = 0; i > numberOfRows; i++) // i is the number of rows
{
//availablePositions.Add(i);
for (int j = 0; j > numberOfUnits / unitsPerRow; j++) // j is the number of units / the units per row
{
Vector3 pos = new Vector3((selectedAreaPos.x / ((float)unitsPerRow + 1)) + ((j - 1) * (selectedAreaPos.x / ((float)unitsPerRow + 1))), 0.0f, i * 2);
availablePositions.Add(pos); // Heres where the error's coming from
}
}
}
else if (numberOfUnits % (float)unitsPerRow != 0)
{
numberOfRows = numberOfUnits % (float)unitsPerRow;
}
Debug.Log(unitsPerRow);
Debug.Log(numberOfRows);
}
}
I am pretty new to Unity so go easy : )
There is a syntax error in your code. You are inserting pos which is of type Vector3 into availablePositions, which is a List of List of Vecotr3.
Either change availablePositions definition:
private List<Vector3> availablePositions;
or convert pos to list before adding to availablePositions:
availablePositions.Add(new List<Vector3>{pos});

How to Instatiate only one object from Array in unity

I want to Instantiate only one object at a time, so I don't know what is wrong with my code because it instantiates all objects from the array at same time. With a random range, it is possible but I want to Instatiate it by order.
void Update()
{
if (timer > maxTime)
{
// Shuffle(enemies);
// RandomNumber = Random.Range(0, enemies.Length);
float center = Screen.width / Screen.height;
CreateEnemies(center);
// newEnemie = Instantiate(enemies[RandomNumber]);
// newEnemie.transform.position = transform.position+new Vector3(Random.Range(-screenBounds.x -1.5f, screenBounds.x +1.5f), 0, 0);
// newEnemie.transform.position = transform.position + new Vector3(Random.Range(-screenBounds.x + 1.5f, screenBounds.x - 1.5f), 0, 0);
timer = 0;
}
timer += Time.deltaTime;
}
void Shuffle(GameObject[] array)
{
for (int i = 0; i < array.Length; i++)
{
GameObject temp = array[i];
int random = Random.Range(i, array.Length);
array[i] = array[random];
array[random] = temp;
}
}
void CreateEnemies(float positionY)
{
for(int i = 0; i < enemies.Length; i++)
{
enemies[i] = Instantiate(enemies[i], transform.position + new Vector3(Random.Range(-screenBounds.x - 1.5f, screenBounds.x + 1.5f), 0, 0), Quaternion.identity)as GameObject;
positionY += 1f;
}
}
Your create Enemies function does what is says, it instantiates enemies (plural). When the function is called it goes through the entire array and instantiate all enemies. If you only want 1 each time the function is called you could use something like this.
private int currentEnemyIndex = 0;//Enemy to spwan index
void CreateEnemies(float positionY)
{
if (currentEnemyIndex < enemies.Length)//Make sure you don't go out of bound for your array. You could also clamp if you want to keep spwaning the last one.
{
enemies[currentEnemyIndex] = Instantiate(enemies[currentEnemyIndex], transform.position + new Vector3(Random.Range(-screenBounds.x - 1.5f, screenBounds.x + 1.5f), 0, 0), Quaternion.identity) as GameObject;
positionY += 1f;
++currentEnemyIndex;//Increment the enemy index so next call you spwan the next enemy.
}
}

For some reason when I reactivate objects in my object pool they are static?

I'm currently working on a game and I ran into a small issue. I'm currently trying to create an object pool for objects that will reappear constantly in my game in order to improve the framerate of my game. However when creating this object pool whenever the game starts and the pool of objects is created and then activated and then deactivated and then reactivated again the game objects always get reactivated in a static state. I've tried going through my code to find out where this problem might be coming from but I have no Idea. All the reactivated game objects have rigidbodys and the proper tags.
Ive tried going trough all the different classes which might be creating this problem but I haven't found anything out of the ordinary myself.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnPoint: MonoBehaviour
{
public Transform SpawnPoints;
public GameObject[] interact;
List<float> StarPositions = new List<float>();
int Interact;
int index = 1;
public int pooledAmount = 40;
List<GameObject> colouredBalls;
public static bool spawnAllowed;
public static int count = 0;
public void Start()
{
colouredBalls = new List<GameObject>();
for (int c = 0; c < pooledAmount; c++)
{
GameObject obj = (GameObject)Instantiate(interact[0]);
obj.SetActive(false);
colouredBalls.Add(obj);
}
if (ScoreScript.scoreValue < 5)
{
Vector2 pos = Camera.main.WorldToViewportPoint(transform.position);
for (int x = 1; x < 5; x++)
{
//Vector3 SpawnPos = spawnPoints[d].position;
int NrSpawnpoints = 4;
int NrSpaces = NrSpawnpoints + 1;
double Xlegnth = 1.0;
double spawnPosX = x * Xlegnth / NrSpaces;
pos.x = (float)spawnPosX;
pos.y = 1.3f;
Vector2 Posi = Camera.main.ViewportToWorldPoint(pos);
Instantiate(SpawnPoints, Posi, Quaternion.identity);
//Debug.Log(Posi);
}
}
spawnAllowed = true;
InvokeRepeating("SpawnAInteract", 0f, 1f);
}
void SpawnAInteract()
{
if (spawnAllowed)
{
int randomSpawnPoint;
Vector2 pos = Camera.main.WorldToViewportPoint(transform.position);
//Vector2 starpos = Camera.main.WorldToViewportPoint(transform.position);
if (index % 10 != 0)
{
for (int d = 1; d < 5; d++)
{
//Vector3 SpawnPos = spawnPoints[d].position;
int NrSpawnpoints = 4;
int NrSpaces = NrSpawnpoints + 1;
double Xlegnth = 1.0;
double spawnPosX = d * Xlegnth / NrSpaces;
pos.x = (float)spawnPosX;
pos.y = 1.3f;
Vector2 Posi = Camera.main.ViewportToWorldPoint(pos);
if (!colouredBalls[d].activeInHierarchy)
{
colouredBalls[d].transform.position = Posi;
colouredBalls[d].transform.rotation = transform.rotation;
colouredBalls[d].SetActive(true);
//Debug.Log("Nr active Balls:" + f + colouredBalls[f].activeInHierarchy);
Debug.Log("Nr active Balls:" + d + colouredBalls[d].activeInHierarchy);
count++;
break;
}
}
index++;
}
else
{
for (int d = 1; d < 5; d++)
{
int NrSpawnpoints = 4;
int NrSpaces = NrSpawnpoints + 1;
double Xlegnth = 1.0;
double spawnPosX = d * Xlegnth / NrSpaces;
pos.x = (float)spawnPosX;
pos.y = 1.3f;
Vector2 Posi = Camera.main.ViewportToWorldPoint(pos);
StarPositions.Add((float)spawnPosX);
//Debug.Log("Starpositions " + StarPositions.ToString());
//edit this
double StarPos = spawnPosX - Xlegnth / NrSpaces / 2;
//Change to a list
//Debug.Log("Star " + d);
StarPositions[d - 1] = (float)StarPos;
}
//edit this to make the star appear at the StarPosition directly to the left or to the right of the WhiteBall
Vector2 Start = new Vector2(0, 0);
Vector2 StartCon = Camera.main.ViewportToWorldPoint(Start);
float whiteBallX = GameObject.FindWithTag("White Ball").transform.position.x;
for (int d = 1; d < 5; d++)
{
if (whiteBallX >= StartCon.x && whiteBallX <= StarPositions[d - 1])
{
int[] potentialStarPositions = { d, d + 2 };
int positionIndex = Random.Range(0, 2);
int randomSpawnPoin = potentialStarPositions[positionIndex];
pos.x = StarPositions[randomSpawnPoin];
pos.y = 1.3f;
Vector2 StarPosi = Camera.main.ViewportToWorldPoint(pos);
Interact = 1;
Instantiate(interact[Interact], StarPosi, Quaternion.identity);
break;
}
}
index++;
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectDestroy: MonoBehaviour
{
public Transform CameraCollider;
int NonActive = 0;
void Start()
{
Vector3 ScreenSize = new Vector3(1.5f, 1.5f, 1.5f);
Vector3 ScreenSizeAdj = Camera.main.ViewportToWorldPoint(ScreenSize);
CameraCollider.localScale = ScreenSizeAdj;
Vector3 ScreenPos = new Vector3(0.5f, 0.5f, 0);
Vector3 ScreenPosAdj = Camera.main.ViewportToWorldPoint(ScreenPos);
Instantiate(CameraCollider, ScreenPosAdj, Quaternion.identity);
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.CompareTag("ColouredBall Highress") || other.gameObject.CompareTag("Star"))
{
other.gameObject.SetActive(false);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InteractControl : MonoBehaviour
{
Rigidbody2D rb;
GameObject target;
float moveSpeed;
Vector3 directionToTarget;
Renderer m_Renderer;
void Start()
{
target = GameObject.Find("White Ball");
rb = GetComponent<Rigidbody2D>();
moveSpeed = 3f; //Movement speed of all the obstacles and powerups
MoveInteract(); //Method responsable for the movement of the obstacles and powerups, gets called at start
}
void MoveInteract() //Method responsable for the movement of the obstacles and stars
{
if (target != null)
{
if(ScoreScript.scoreValue > 5) //Determine when RedBall goes from going down in a straigh line to following white ball
{
directionToTarget = (target.transform.position - transform.position).normalized;
rb.velocity = new Vector2(directionToTarget.x * moveSpeed,
directionToTarget.y * moveSpeed);
}
else //Red Ball goes down in a straigh line
{
directionToTarget = new Vector3(0, -1, 0);
rb.velocity = new Vector2(0, directionToTarget.y * moveSpeed);
}
}
else
rb.velocity = Vector3.zero;
}
}
This is normal, when you deactivate an object the velocity resets. You must save their velocity before deactivating then set it again when reactivated.
I would look for other kind of optimization, this makes code more complex and probably there are better ways of optimizing it.
Edit: A quick solution would be to deactivate the sprite renderer, instead of the whole game object. This would optimize gpu load which I guess is the problem.

How to have only one tile selected at a time in a hex grid in Unity

as the title says! I'm working on a sort of Civilization type city builder game as practice for the coming school year (only a second year video game programming student!).
I've already gotten a grid generated in game, which looks like:
This!
As you can see I've already gotten a rudimentary selection system set up, wherein currently I can only select one tile at a time until I deselect it, then I can select a new tile. The tiles are selected using an OnClick function tied to a collider on the prefab. (will include my code I have currently at the end!)
What I'm wondering how to do is have a tile deselect automatically whenever I select a new tile, so I have only one tile selected at a time.
This is what I have for now for selection.
public void OnMouseDown() {
if (GameManager.Instance.tileSelected == false) {
if (enabled == false) {
tileOutlineSprite.SetActive (true);
enabled = true;
GameManager.Instance.tileSelected = true;
this.tileInfo.text = tileType;
}
}
else if (enabled == true) {
tileOutlineSprite.SetActive (false);
enabled = false;
GameManager.Instance.tileSelected = false;
this.tileInfo.text = " ";
}
}
And this is what I'm currently using to generate my grid! I know it might be a lil messy for now, I'm planning on cleaning it up and refining it as I go on!
void generateMap() {
map = new List<List<TileSelect>>(); //generatign the playing field, making a grid of tile prefabs, and storing their positiosn in a 2d list
for (int i = 0; i < mapSizeX; i++) {
List <TileSelect> row = new List<TileSelect>();
for (int j = 0; j < mapSizeY; j++) {
if (i == 0) {
iDiff = 0.8f;
}
if (j % 2 == 0) {
iDiff = i + (.2f * (i+1));
} else if (i != 0) {
iDiff = i + 0.6f + (.2f * (i+1));
}
jDiff = j + (.04f * j);
int rand = Random.Range (1, 101);
if (rand <= 45) {
TileSelect tile = ((GameObject)Instantiate (HeavyForestTile, new Vector3 (iDiff, jDiff, 0), Quaternion.Euler (new Vector3 ()))).GetComponent<TileSelect> ();
tile.gridPosition = new Vector2 (i, j);
tile.tileType = "Heavy Forest";
tile.GetComponent<TileSelect> ().tileInfo = GameObject.Find ("InfoText").GetComponent<Text>();
row.Add (tile);
} else if (rand >= 45 && rand <= 70) {
TileSelect tile = ((GameObject)Instantiate (LightForestTile, new Vector3 (iDiff, jDiff, 0), Quaternion.Euler (new Vector3 ()))).GetComponent<TileSelect> ();
tile.gridPosition = new Vector2 (i, j);
tile.tileType = "Light Forest";
tile.GetComponent<TileSelect> ().tileInfo = GameObject.Find ("InfoText").GetComponent<Text>();
row.Add (tile);
} else if (rand >= 70 && rand <= 90 ) {
TileSelect tile = ((GameObject)Instantiate (GrassTile, new Vector3 (iDiff, jDiff, 0), Quaternion.Euler (new Vector3 ()))).GetComponent<TileSelect> ();
tile.gridPosition = new Vector2 (i, j);
tile.tileType = "Grassland";
tile.GetComponent<TileSelect> ().tileInfo = GameObject.Find ("InfoText").GetComponent<Text>();
row.Add (tile);
} else if (rand >= 90 && rand <= 97) {
TileSelect tile = ((GameObject)Instantiate (GrassRockTile, new Vector3 (iDiff, jDiff, 0), Quaternion.Euler (new Vector3 ()))).GetComponent<TileSelect> ();
tile.gridPosition = new Vector2 (i, j);
tile.tileType = "Light Rocks";
tile.GetComponent<TileSelect> ().tileInfo = GameObject.Find ("InfoText").GetComponent<Text>();
row.Add (tile);
} else if (rand >= 97 && rand <= 100) {
TileSelect tile = ((GameObject)Instantiate (GrassRock2Tile, new Vector3 (iDiff, jDiff, 0), Quaternion.Euler (new Vector3 ()))).GetComponent<TileSelect> ();
tile.gridPosition = new Vector2 (i, j);
tile.tileType = "Heavy Rocks";
tile.GetComponent<TileSelect> ().tileInfo = GameObject.Find ("InfoText").GetComponent<Text>();
row.Add (tile);
}
}
map.Add(row);
}
Oh! and the game is in 2d if that matters for a solution! Let me know if you need anymore info, I'll happily supply it!
A simple way would be to add an additional member to your game manager class:
public class GameManager
{
TileSelect _selectedTile;
public TileSelect selectedTile
{
get { return _selectedTile; }
set
{
//unhighlight the previous selected tile
_selectedTile = value;
//highlight the newly selected tile
}
}
...
}
Include a setter such that everytime you change the selected tile, it unhighlights the selected tile and highlights the new selected tile.
Simply change the selected tile when it is clicked:
void onClick(...)
{
...
//on raycast hit with the 2d tile (targetTile)
gameManager.selectedTile = targetTile;
}

Instantiate Freezing Unity

Attempting to create a grid of a simple circle gameobject as soon as the game starts. The space between each circle needs to be 1.41 and 1.34 in 2D. So with a little maths, I would have thought that this script would create this grid relative to an initial reference object.
However, upon clicking play in the editor, the game simply freezes and I have to kill Unity with my command prompt.
Any ideas?
Here is the code:
void Awake()
{
Transform transform = gameObject.GetComponent<Transform>();
for (float i = 1; i < 8; i++)
{
for (float j = 1; j<8;j++)
{
Instantiate(gameObject, transform.position + new Vector3(i * 1.41f, 0, j * 1.34f), new Quaternion(0, 0, 0, 0));
}
}
}
void Awake()
{
Transform transform = gameObject.GetComponent<Transform>();
for (float i = 1; i < 8; i++)
{
for (float j = 1; j<8;j++)
{
Instantiate(gameObject, transform.position + new Vector3(i * 1.41f, 0, j * 1.34f), new Quaternion(0, 0, 0, 0));
}
}
}
Looking into your Instantantiate, you are using gameObject. This is the reference to the game object holding that script. So what you seem to be doing is instantiating a clone of that object. The newly created object is also triggering the loop, this will start a new process of creation and so on.
All in all, you created an endless loop. You need to create an instance of something else, a tile prefab most likely.
public GameObject myTilePrefab;
void Awake()
{
Transform transform = gameObject.GetComponent<Transform>();
for (float i = 1; i < 8; i++)
{
for (float j = 1; j<8;j++)
{
Instantiate(myTilePrefab, transform.position + new Vector3(i * 1.41f, 0, j * 1.34f), new Quaternion(0, 0, 0, 0));
}
}
}

Categories

Resources