StackOverflow on Instantiate
Hi everyone, I started Unity and C# by the same occasion like two days ago, and I got an issue with a stackoverflow when I try to instantiate a prefabs. Everything was working juste fine but then I tried to be clean by seperate things into different class/interface and then the problem occured. Here is the differents part :
public class Game : MonoBehaviour
{
public ILevel level;
public void Start()
{
this.level = GameObject.FindWithTag("MainCamera").GetComponent<ILevel>();
this.level.InitBoard();
}
}
public interface ILevel
{
Tile FindTileByPosition(float x, float y);
void InitBoard();
List<PlayerScript> InitPlayer();
}
public class Level01 : MonoBehaviour, ILevel
{
private List<float[]> levelParameter;
void Start()
{
this.levelParameter = new List<float[]>
{
new float[] { 1, 1, 1, 1, 1.5F, 2 },
new float[] { 1, 1, 1, 1, 1, 2 },
new float[] { 1, 1, 1, 1, 1, 1 },
new float[] { 1, 1, 1, 1, 1, 1 },
new float[] { 1, 1, 1, 1, 1.5F, 2 },
new float[] { 2, 2, 2, 2, 2, 2 }
};
}
public void InitBoard()
{
Builder builder = GameObject.FindWithTag("MainCamera").GetComponent<Builder>();
if (builder.Init())
{
//this.lTile = builder.Build(this.levelParameter);
builder.Build(this.levelParameter);
}
}
[...]
}
public class Builder : MonoBehaviour
{
private GameObject prefabs1x1;
private GameObject prefabs2x1;
public bool Init()
{
this.prefabs1x1 = Resources.Load("Prefabs/1x1") as GameObject;
this.prefabs2x1 = Resources.Load("Prefabs/2x1") as GameObject;
if (this.prefabs1x1 == null || this.prefabs2x1 == null)
{
Debug.Log("IT'S NULL!!!");
return false;
}
return true;
}
//public List<Tile> Build(List<float[]> map)
public void Build(List<float[]> map)
{
float x = 0;
float z = 0;
//List<Tile> lTile = new List<Tile>();
foreach (float[] ligne in map)
{
foreach (float height in ligne)
{
if (height == 1)
{
//lTile.Add(Instantiate(this.prefabs1x1, new Vector3(x, 0.5F, z), Quaternion.identity).GetComponent<Tile>());
Instantiate(this.prefabs1x1, new Vector3(x, 0.5F, z), Quaternion.identity);
}
else
{
//lTile.Add(Instantiate(this.prefabs2x1, new Vector3(x, 0.5F, z), Quaternion.identity).GetComponent<Tile>());
Instantiate(this.prefabs2x1, new Vector3(x, 0.5F, z), Quaternion.identity);
}
x++;
}
x = 0;
z++;
}
//return lTile;
}
At first Unity just crashed every time I was launching the game but some rare time the console had time to show a stackoverflow error before unity crash. I'm confused because it's work just fine when I comment the instantiate line. Sorry for the long post.
Answer by streeetwalker · Apr 05, 2020 at 03:15 PM
@Kazadri , are we looking at code here that works, and the parts that don't work are commented out? Or this code does not work as is?
Generally, if you are getting a stack overflow error it means you have recursion going on that never returns.
This looks suspicious: in the Level01 class, where do you declare this.lTile ?
Sorry for the confusion ^^ The code does not work as is, the comment part are just test (I should have erase it for the post). The code work if I comment this two lines :
Instantiate(this.prefabs1x1, new Vector3(x, 0.5F, z), Quaternion.identity);
Instantiate(this.prefabs2x1, new Vector3(x, 0.5F, z), Quaternion.identity);
For the this.lTile in Level01 class, I forgot to erased the comment but erased the declaration :
private List<Tile> lTile;
But the code doesn't work even when I comment it :/
@Kazadri, I bet, somewhere, somehow you have recursion happening. Tell me that each of your prefabs does not have a script on them that instantiates more prefabs. Do you have any code that runs on your prefabs?
I actually have some code in my prefabs but none that instantiates more prefabs, here are the code on my prefabs :
public class Tile : $$anonymous$$onoBehaviour
{
public Vector3 pos
{
get { return pos; }
set { pos = value; }
}
private float size;
private Color m_$$anonymous$$ouseOverColor = Color.green;
private Color m_OriginalColor;
private $$anonymous$$eshRenderer m_Renderer;
private List<PlayerScript> lPlayer;
// Start is called before the first frame update
void Start()
{
this.pos = transform.position;
this.size = transform.localScale.y;
this.m_Renderer = GetComponent<$$anonymous$$eshRenderer>();
this.m_OriginalColor = m_Renderer.material.color;
this.lPlayer = new List<PlayerScript>();
}
// Update is called once per frame
void Update()
{
}
//[...]
}
Your answer
Follow this Question
Related Questions
Instantiate and destroy an object with the same key 1 Answer
How do I destroy a instantiated UI image that is a prefab but pushed the canvas? 0 Answers
instantiated prefab doesn't follow spawn rotation 1 Answer
Instantiating prefabs from an array of GameObjects 3 Answers
Spawn in a different and random Spawn point from a list 0 Answers