I’m trying to write a single script that will be placed on an empty gameObject in my scene. this script will have 10 different locations for blocks to be instantiated at. this is what i have so far
using UnityEngine;
using System.Collections;
public class SpawnPositions : MonoBehaviour
{
public GameObject Spawner;
// Use this for initialization
void Start ()
{
Spawner = (GameObject)Instantiate(newObject,
new Vector3 Quaternion(0f, 0f, 0f, 0f), transform.rotation);
}
// Update is called once per frame
void Update ()
{
}
}
what i need it to do is to have 10 locations that i can have another script randomly select to instantiate an object at. i have created a GameObject called Spawner to hold this one script so i can have access to it. but I’m not sure on how to do this. i kind of saw something like this once but don’t remember how it was set up?
what you need is an array of Transform locations then a random generator that gives you a number between 1 and 10 then select the spot in the array of what the random number is and spawn the item there.
look around and try and do this for yourself first, if no one else posts something that helps and you can’t get it ill try and post something tomorrow because im tired and anything i post right now will not work lol. but please seriously try it for yourself instead of just waiting on me to do it. (:
void Start ()
{
Spawner = (GameObject)Instantiate(newObject, new Vector3(x, y, z), new Quaternion(x, y, z, w));
}
Here is the part from my main script that i want to call the one above
public GameObject[] Blocks;
public GameObject[] SpawnLocations;
// this is called in the update on the current state for the game
for ( int i = 0; i < SpawnLocations; ++i)
{
Random random;
while(Blocks!=SpawnPositions)
{
random = new random();
}
newObjcet = (gameObject)Instantiate(Blocks[random]);
newObject.transform.position = SpawnLocations[i].transform.position;
}
If you just want to spawn a single object at a random location, you can do this with Random.Range:-
var spawnPos = SpawnLocations[Random.Range(0, SpawnLocations.Length].transform.position
If you want to spawn several objects at different random locations (but never use the same one twice), you will probably find the randomisation script in this thread useful.
I need them to spawn at locations i have hard coded into the script, but pick them at random, but here is the catch. what im trying to accomplish here is that there are specific letters that i need to spell a word on the screen, and depending on how many letter are in that word, i need the rest to be filled with random letters to fill the rest of the positions on the screen that are in the spawn script. I only want ten locations to be made. so say the word is apple, then i would need the blocks a, p, p, l, and e to be placed in random positions out of those ten positions on the spawn script, and then have the leftover 5 filled with random blocks.
I have been going over this in my head and figured out what i think i need to do.
What i need to do is to have this set up to be state specific. by that i mean i have different states set up in my game currently that determine what word im trying to spell.
what i think needs to happen is at the start of the state have the blocks i need to spell the word hard coded to instantiate from my array of blocks, but i want them to be instantiated at a random location from the ten i have in an array that are attached to empty gameObjects. there needs to be a way to determine if those blocks in the array i have, have been selected or turned to “selected” with a bool so that when it comes time to fill the leftover positions on the screen, the same block will not be selected again. this same logic needs to occur for the locations too, i dont want to use the same location twice.
would this maybe work for putting a GameObject into a random location with in my ScreenSpawnLoc array? For some reason length or count wont work after ScreenSpawnLoc?