Question by
adrian2256 · Aug 10, 2020 at 06:52 PM ·
unityeditorprogramming
Instantiate Method won't work a second time.
This is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.HighDefinition;
public class ComponentScript : MonoBehaviour
{
public bool enabledTile = false;
public Vector3 position;
public bool selected = false;
public bool floor = false;
public Material floorMat;
public Material sel;
public Material testMat;
//
public MeshRenderer mr;
public BoxCollider bc;
public Select sl;
private Grid grid;
private MeshFilter mf;
private Meshes msh;
public void Awake()
{
grid = GetComponentInParent<Grid>();
mf = GetComponent<MeshFilter>();
msh = GetComponentInParent<Meshes>();
if (floor) { GetFloorPlacement(); }
}
public void GetFloorPlacement()
{
int x = (int)position.x;
int y = (int)position.y;
mf.mesh = msh.Floor_top;
//Esquinas
if (x == 0 && y == 0)
{
grid.DebugDrawBox(5, new Vector3(x - 1 * grid.cellSize, 0, y - 1 * grid.cellSize), 2);
//Corner
Debug.Log("Create Corner");
var corner = Instantiate(grid.defaultGameObject, new Vector3((x - 1) * grid.cellSize, 0, (y - 1) * grid.cellSize), Quaternion.identity, grid.transform.Find("Grid Objects").transform);
corner.name = "Corner x0y0";
corner.transform.localScale = new Vector3(grid.cellSize, grid.cellSize, grid.cellSize);
}
}
}
Whenever I create a GameObject containing this script, It will create the corner perfectly. But, If I delete it, and Instantiate it again just the same way I did with the previous one, the corner does not appear. The Debug.Log that is just before the Instantiate method does work perfectly, tho I dont know why.
I have searched for quite a long time over the Internet and some other Discord servers, and I havent found a solution yet.
Thanks for reading!
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613005341im_/https://answers.unity.com/themes/thub/images/avi.jpg)