- Home /
Random array spawn C#
I am trying to get an object to spawn on start at a random spot. I have made 3 empty game objects for the spots that the object would spawn, and made the object a prefab and connected them to the prefab. here is the full code i have so far in c#
using System.Collections;
using UnityEngine;
public class random : MonoBehaviour {
public Transform[] spawnLocation;
public GameObject[] objectToSpawn;
public GameObject[] spawn;
int randomInt;
void Start() {
spwanSomething();
randomInt = Random.Range(0, 3);
}
void spwanSomething() {
spawn[randomInt] = Instantiate(objectToSpawn[randomInt], spawnLocation[randomInt].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
spawn[randomInt] = Instantiate(objectToSpawn[randomInt], spawnLocation[randomInt].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
spawn[randomInt] = Instantiate(objectToSpawn[randomInt], spawnLocation[randomInt].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
}
}
if you need more info, please let me know.
Thank you
You spawn random things before you set randomInt. Swap the statements in the Start() function around.
Also I would use:
randomInt = Random.Range(0, spawn.length - 1);
So you don't have to worry about hard coding the length of the array. You might also want to do that before each thing you spawn in spawnSomething(), so you get a different random object each time.
Answer by DarkToadster · Mar 16, 2018 at 07:09 AM
i think this might help you. Ask if you have any questions. You may get problems if you spawnlocations are child Objects. If you have, try to modify this with an Vector3 array.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnAtRandomLocation : MonoBehaviour
{
public GameObject[] objectsToSpawn;
public Transform[] spawnPoints; // An Vector3 array can also be used
public List<GameObject> spawnedObjects; // Containing all spawned Objects; Using List to simply call .Add(GameObject);
public int spawnCount; // How many objects should be spawned
private int objectIndex; // Random objectsToSpawn index
private int spawnIndex; // Random spawnPoints index
private void Start()
{
// Use this for loop to not hardcode the spawn count
for (int i = 0; i < spawnCount; i++)
{
// For each iteration generate a random index; You could make an int array containing if an object already got spawned and change the index.
objectIndex = Random.Range(0, objectsToSpawn.Length);
spawnIndex = Random.Range(0, spawnPoints.Length);
// Instantiate object
GameObject go = Instantiate(objectsToSpawn[objectIndex], spawnPoints[spawnIndex].position, Quaternion.identity);
// Add Object to spawnedObjects List
spawnedObjects.Add(go);
}
}
/// <summary>
/// Draws a Sphere at each spawnPoints position
/// </summary>
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
for (int i = 0; i < spawnPoints.Length; i++)
{
Gizmos.DrawSphere(spawnPoints[i].position, 0.5f);
}
}
}
an issue came up right off the bat.... " GameObject go = Instantiate(objectsToSpawn[objectIndex], spawnPoints[spawnIndex].position, Quaternion.identity); " with that section of the code. Unity says that i cant Instantiate within that section. here are the errors that come up (picture attached) Please help, thank you
That script does not derive from $$anonymous$$onobehaviour.
public class NewBehaviourScript : $$anonymous$$onoBehaviour { }
ok so how do I use that code? what do I attach it to?
when I put it in it doesnt work at all?
all the errors were fixed with the public class being changed. but i dont understand how to work with it in unity. what do i attach the code to...how do i use it?
Create a script called "SpawnAtRandomLocation" and copy all my code in it. This will work. You also could copy the code inside the class into another script. But i strongly recommend you to watch some Basic C# and Untiy tutorials! Learning by doing is great but can lead you in some wrong ways. And spending all time in Forums with very basic questions would take almost forever to finish any game :)
The issue is not how to implement the code into unity, it is how to get the code to work with the object. Can you tell me how I would do that?
and I have watched all the Basic C# videos for unity as well as a youtube series about c# that was 15+ hours long. I just am not getting it, it is very difficult for me to understand. I don't want you yo $$anonymous$$ch me how to script, i would just like to figure this one thing out. I can trudge my way though learning it later, but i need to know how this random script works.
I have attached it to an empty game object with no child objects and then made 3 empty game objects and used those for spawn locations. and it did not work. so then i made the 3 spawn points if you will (the empty game objects) a child of the empty game object that has the script attached. and that did not work. Than i attached the script to player, to another object in the game, and to the spawn points, all of those did not work.
so...how am i supposed to use the script? What do i attach it to? how to i get it to work?
i would really appreciate some help with this aspect.
thank you.
Thank! This Script Work For me, But can you help me how to fix some of location have more than 1 object
Your answer
![](https://koobas.hobune.stream/wayback/20220612154707im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Random Children Array that changes Parent, not working right 0 Answers
Randoim values in a dynamic array C# 1 Answer
How to make array to non-same random value? 2 Answers
Unable to randomly instantiate prefab from array 2 Answers
Pick between two floats 2 Answers