Problems with instantiating
Hi guys, I'm trying to create a random loot chest but when I try to instantiate the prefab the location is always messed up, but when I tried with a new gameobject it worked fine and I can't understand why ...
here's the code: public GameObject[] Loot; public Transform LootSpawnPoint;
int random;
private void OnCollisionEnter(Collision other)
{
random = Random.Range(0, Loot.Length);
if(other.collider.CompareTag("Player"))
{
Instantiate(Loot[random], LootSpawnPoint.transform.position, Quaternion.identity);
}
}
and a pic (he shoe is the prefab that should have spawned in the gameobject position):
How did you assign the loot Objects in the Gameobject Array?
Answer by unity_03phillipsm · Sep 23, 2020 at 11:31 AM
Two things spring to mind, but I don't know if either helps:
If LootSpawnPoint is of type Transform, do you need to call
LootSpawnPoint.transform.position
, or justLootSpawnPoint.position
?I think
Random.Range(0, 1)
can return both 0 and 1, therefore you might have an out of bounds error callingLoot[random]
(So if Loot.Length is 1, you could be accidentally trying to call the second, non-existent element of Loot)?
ehm the problem is none of these ... it must be the prefab pivot that is messed up, how can I fix it?