- Home /
Start function is getting called twiced
Im currently working on a room generation system and i have a script which adds the boxcollidor. It looks like this:
void Start(){
BoxCollider2D bottomLeft =
setSettings(bottomLeft, verticalSize, minimumSize, -verticalOffsetX, -verticalOffsetY);
}
void setSettings(BoxCollider2D collidor, float xsize, float ysize, float xoffset, float yoffset){
collidor.size = new Vector2(xsize, ysize);
collidor.offset = new Vector2(xoffset, yoffset);
}
This script is attached to a Room gameobject. The room gameobject is getting instantiated through a level generation script which has multiple rooms in it. So basically i do Instantiate(rooms[i], location, rotation) in a for loop in the level generation script. When i run the game and look at the room it now has every boxcollidor i assigned twice(there is only one object). I have no idea why this happens. It seems like the start function of the boxcollidor is getting called twice but i dont know why.
Please don't claim wild assumptions which can be easily verified by placing a Debug.Log inside Start. Are you sure that your prefab doesn't already contain a BoxCollider?
Start is only called once by Unity. It could be that you somehow call it manually again somewhere?
Try adding this to your Start method:
Debug.Log("Start of " + gameObject.name, gameObject);
Each debug log entry in the console also has a stacktrace which might help to figure out where the other calls might come from (if it's actually called twice).
Your code in your question is missing your "BoxCollider creation" so it's pretty pointless
Ok I solved it. I had the room i was creating in the scene so it was creating its own boxcollidors and Instantiate was creating a copy of the object(with the boxcollidors). I forgot that instantiate doesnt create a new object. Oops