I’m trying to spawn three different collectable items (they’re prefabs by the way) in a large 3x5 grid at specific areas in a map. I don’t know where to start though because I’m trying to use the instantiate function but I keep running into errors or crashing unity. Does anyone know any tutorials or a good place to start? (No javascript please.)
What’s your problem here, or the errors you get?
There’s no problem with instantiating your prefabs over the map and using collider with OnTriggerEnter to pick the up and then destroying it after to remove them from the map, works just fine. (If you only spawn the same items and only 1 or 2 at once from the same type, you could already place them beneath the map and just move them back and forth (or disable renderer/collider when picked up)
The problem is that I need to have about 90 copies of 4 different collectible items in a level and I don’t want to manually place them in the scene. If possible, I would rather just instantiate and place them through code. Is it possible and if so, where do I start?
You’ll start with a prefab, that holds your item. then just instantiate that prefab over the world. For the position you have to set them, you could use a Vector3 and cange some values x+y via Range.Random. Or define some areas where they could spawn. That’s pretty much it, no real magic going on here, just instantiate them on a position that you’ll define. If you want to have random positions use Random.Range(fromValue,toValue) within the positions coordinates (x,y,z) that’s it. You can also use the Random.Range to choose one of the 4 items you have.