Spawning Random Objects?
My game object is a crate. I also have another object. When I keep going, I want either one of those objects to spawn in a specific location. How can I implement something like through coding?
Answer by Lilius · Oct 13, 2016 at 11:10 AM
Put your objects in an array, get a random index for that array and instantiate that object:
public GameObject[] myObjects;
int randomIndex = Random.Range(0, myObjects.Length);
GameObject instantiatedObject = Instantiate(myObjects[randomIndex], position, Quaternion.identity) as GameObject;
When I enter this code, it gives an error saying that it can no convert object to Unity.Object. Also, do I add this code to the main player?
Or did it say can not convert Object[] to Unity.Object?
You can add it in any object you need to add it, I don't know your scene setup. What is your problem at the moment? Do you have difficulties in the part where you need to randomly decide which object to instantiate (which is what you asked for) or are you missing the whole concept? Do you have the code to decide when to instantiate one of those objects? Do you have the code to decide where to instantiate those objects? Show the whole script what you have so far and I'm happy to try to assist. The concept of what you are trying to do is rather simple, but you don't even tell what language you use so it's impossible to give a block of code that you can just copy paste.
So the character is walking on a horizontal plane. There are two objects. While the player is walking, I want either one of those objects to spawn in a specific location on the plane. I use C#. I am missing the whole concept. And yes, it does say "can not convert Object[] to Unity.Object".
Answer by JScotty · Oct 14, 2016 at 07:46 AM
I'd do:
public class ClassName : MonoBehaviour {
[SerializeField] private ObjectInfo[] objects; // fill in editor
GameObject GetRandomObject(ObjectInfo[] objects){
float chance = Random.Range(0,100); // random (0 to 99) %
foreach (ObjectInfo obj in objects) {
// Check if random is in chance
if (chance < obj.chance) {
return obj.obj; // returns object
}
// Fix chance for next item
chance -= (int)item.chance;
}
}
}
[System.Serializable]
public struct ObjectInfo{ // structure for object information
public GameObject obj; // Prefab
public short chance; // (0 to 99) %
}
this is an great method for readable code and to have a nice structure in your editor.
ofcourse to spawn an object just do:
Instantiate(GetRandomObject(objects)); // single use
GameObject spawnObj = Instantiate(GetRandomObject(objects)); // for usage of spawned object
Answer by SohailBukhari · Oct 13, 2016 at 01:56 PM
@username When you instantiate random objects it is possible that two same objects instantiate. So , Take a list of random numbers and choose one number and remove that number from list , when your list become empty then refill it using recurrsion. List randomNumber = new List ();
int RandomNumberChooser ()
{
if (randomNumber.Count == 0) {
Fill_List ();
RandomNumberChooser ();
}
var index = Random.Range (0, randomNumber.Count);
var value = randomNumber [index];
randomNumber.RemoveAt (index);
return value;
}
void Fill_List ()
{
for (int i = 0; i < targetArrays.Count; i++) {
randomNumber.Add (i);
}
}
Instantiate your objec at return value from RandomChooser Method Like this SelectedRandomNumber = RandomNumberChooser (); GameObject instantiatedObject = Instantiate(myObjects[SelectedRandomNumber ], position, Quaternion.identity) as GameObject;
Your answer
Follow this Question
Related Questions
Random object placing parameters 0 Answers
Cannot convert 'object' expression to 'float' 1 Answer
C# Find specific object by getting one of its variables 0 Answers
C# Limit touch movement on a raycast 2 Answers
2D - Object follow cursor on X-axis. 0 Answers