- Home /
Prefab not instatiating at correct position
So Im trying to instantiate a unit with a z offset of 5 from the factory instantiating it but the new object just goes to (0,0,5) instead of the factorys transform.position + offset.
public class SpawnInfantryUnits : MonoBehaviour
{
public GameObject lesserDeathUnit;
public GameObject lesserWarUnit;
public static GameObject lesserDeath;
public static GameObject lesserWar;
Vector3 spawnSpot;
public Vector3 posOffset;
private void Start()
{
spawnSpot = transform.position;
print(spawnSpot);
spawnSpot += posOffset;
print(spawnSpot);
}
private void OnMouseDown()
{
UIManager.InfantryUnits.SetActive(true);
UIManager.Research.SetActive(false);
UIManager.buildMain.SetActive(false);
UpgradeSupply.upgradeSupply.SetActive(false);
UIManager.Min.SetActive(true);
// UIManager.unitMain.SetActive(false);
}
public void SpawnLesserDeath()
{
if (Stats.population < Stats.maxPopulation)
{
print("pop check pass");
print(spawnSpot);
if (Stats.money >= 125)
{
lesserDeath = (GameObject)Instantiate(lesserDeathUnit, spawnSpot, Quaternion.identity);
//lesserDeath.transform.parent = gameObject.transform;
Stats.units.Add(lesserDeath);
Stats.money -= 125;
print("Death Spawn");
}
else
{
BuildingSelect.SupplySelected = false;
print("Too poor");
}
}
}
public void SpawnLesserWar()
{
if (Stats.population < Stats.maxPopulation)
{
print("pop check pass");
if (Stats.money >= 125)
{
lesserWar = (GameObject)Instantiate(lesserWarUnit, transform.position + posOffset, Quaternion.identity);
//lesserWar.transform.parent = gameObject.transform;
Stats.units.Add(lesserDeath);
Stats.money -= 125;
print("War Spawn");
}
else
{
BuildingSelect.SupplySelected = false;
print("Too poor");
}
}
}
}
In start spawnSpot prints the desired position then prints 0, 0, 5 in SpawnLesserDeath(). it changes and I'm not sure why?
Edit: and since it changes it doesn't instantiate at the desired position
Is the factory start position (0,0,0)?? If that is the case, you will always get (0,0,5). You would need to update the position inside SpawnLesserDeath() like you are doing in the instantiate method in SpawnLesserWar()
Answer by jwalkaz33 · Apr 22, 2020 at 05:53 AM
when it was instantiating it was taking the prefab of the spawner object instead of the one that was in the scene so I had to get a reference to the one in the scene then use its transform
public void SpawnLesserDeath()
{
if (selectedBuilding.currentBuilding.Count > 0)
{
if (Stats.population < Stats.maxPopulation)
{
// print("pop check pass");
if (Stats.money >= 125)
{
lesserDeath = (GameObject)Instantiate(lesserDeathUnit, selectedBuilding.currentBuilding[0].transform.parent.parent.position + posOffset, Quaternion.identity);
Stats.units.Add(lesserDeath);
Stats.money -= 125;
//print("Death Spawn");
}
else
{
BuildingSelect.SupplySelected = false;
//print("Too poor");
}
}
}
}
getting the object in the scene
public static List<GameObject> currentBuilding = new List<GameObject>();
private void Update()
{
if (Input.GetMouseButton(1))
{
currentBuilding.Clear();
}
}
private void OnMouseDown()
{
if (currentBuilding.Count > 0)
{
currentBuilding.Clear();
currentBuilding.Add(gameObject);
print(currentBuilding.ToString());
print("game object removed and new added" + gameObject.name);
}
else
{
currentBuilding.Add(gameObject);
print(currentBuilding.ToString());
print("game object added" + gameObject.name);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612231538im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I position an instantiated row of prefabs? 1 Answer
Wrong particle and prefab spawn position 1 Answer
Stop Children of Instantiated Parent From Offsetting After Changing Position 0 Answers
Prefabs dont instantiate directly on point 0 Answers
Referencing gameObject from script after Instantiate 0 Answers