My instantiations are spawning all kinds of sizes clones especially larger ones and i dont know why? 20x the size at least
My instantiations are spawning all kinds of sizes clones especially larger ones and i dont know why? 20x the size at least
I checked the prefab its small size
I did try the script on another object and it works fine with another object so why with this prefab I have does it clone all kinds of sizes clones and 20x larger ones at times?
please help
2d game
without knowing what your code is doing, there's no way one can help
using UnityEngine;
using System.Collections.Generic;
using Random = UnityEngine.Random;
public class crashspawn : $$anonymous$$onoBehaviour {
public GameObject SpawnItem;
public bool ControllerActive = false;
public List<GameObject> SpawnedObjects;
public int SpawnAtOnce = 1;
public int $$anonymous$$axSpawnedObjects = 2;
public float SpawnEatch = 3;
public bool OverSpawn = false;
private float spawnTimeLeft;
private Vector3 spawnPosoition
{
get
{
return new Vector3(
Random.Range(0f, 10f),
Random.Range(0f, 10f),
Random.Range(0f, 10f));
}
}
void Start ()
{
SpawnedObjects = new List<GameObject>($$anonymous$$axSpawnedObjects);
spawnTimeLeft = SpawnEatch;
}
void FixedUpdate()
{
for (var i = 0; i < this.SpawnedObjects.Count; i++)
{
if (SpawnedObjects[i] == null)
{
this.SpawnedObjects.RemoveAt(i);
}
}
if (ControllerActive)
{
if (spawnTimeLeft <= 0)
{
if (SpawnedObjects.Count < $$anonymous$$axSpawnedObjects)
{
if (OverSpawn)
{
// Debug.Log("Over Spawning " + SpawnAtOnce + " Objects");
for (int i = 0; i < SpawnAtOnce; i++)
{
var o = (GameObject)Instantiate(SpawnItem, spawnPosoition, Quaternion.identity);
o.transform.parent = this.transform;
SpawnedObjects.Add(o);
}
}
else
{
var itemsAllowedToSpawn = $$anonymous$$axSpawnedObjects - SpawnedObjects.Count ;
var itemsToSpawn = $$anonymous$$athf.Clamp(itemsAllowedToSpawn,0,SpawnAtOnce);
//Debug.Log("Spawning " + itemsToSpawn + " Objects");
for (int i = 0; i < itemsToSpawn; i++)
{
var o = (GameObject)Instantiate(SpawnItem, spawnPosoition, Quaternion.identity);
o.transform.parent = this.transform;
// Add Spawned Objects to List
SpawnedObjects.Add(o);
}
}
}
spawnTimeLeft = SpawnEatch;
// Debug.Log("DoneSpawning");
}
spawnTimeLeft -= Time.deltaTime;
}
}
}
Is it possible you're instantiating UI elements or something? if so, you cannot use the parent = transform way of childing them but you need to use SetParent(transform, false) ins$$anonymous$$d
@sid4 usually when you put an object as the child of another one it rescale according to the parent scale. So one think I would look is that your parent has Scale (1,1,1) and just to be sure on the child object you could try o.transform.localScale = new Vector3(1, 1, 1);
after you parent it. Of course I am assu$$anonymous$$g that spawn objects have an initial scale of (1,1,1). If not just save the scale after you instantiate them and reassign as above.
i just tried this it worked so far I must test more
can you explain to me why this happened? and more of what you mean I did not understand
Hi @sid4 , every time you parent an object to another one you put it into his reference system. That is composed by it' s position, orientation and scale. Every time you parent one to the other there is an inner transformation matrix that, taking in consideration parent properties, get multiplied by the position, rotation and scale of the child object. Therefore intuitively if you have a child object with scale (1,1,1) and you parent to an object with scale (1,2,1), once parented the child object will have double of the size on the y axes with respect to its original size. If you want to know more, have a look at Chapter 7 of this book to have a better idea. This is the base maths that a game programmers need: $$anonymous$$ath 3d Primer
Your answer
Follow this Question
Related Questions
NetworkServer.Spawn() make client crash to desktop when called in a loop 1 Answer
Check if there's no GameObject with specific tag in the field (in the camera view) 0 Answers
How to make Object X spawn when there're no more Objects tagged "XYZ" in scene? 1 Answer
spawn in specific areas only 0 Answers