- Home /
Spawn A Set Amount of Objects at Set Location
I have been all over the internet trying to figure this out. I have 13 fruit, and I want them to spawn where the watermelon is. I want the watermelon to have a 12/86 chance of spawning, the strawberry 11/86 chance of spawning, and etc. I want to make it so every 2 seconds it spawns an object at that set location(where the watermelon is)and the fruit will fall in case you where wonder. Thank you in advance. Script is appreciated.
Answer by HenryChinaski · Jul 10, 2015 at 02:12 AM
C#
public GameObject spawnPoint;
public GameObject watermelon;
public GameObject strawberry;
public GameObject apple;
etc..
void spawnFruit()
{
int whichFruit = Random.Range(1,100);
GameObject thatFruit;
thatFruit=apple;
if (whichFruit > 10) thatFruit=strawberry;
if (whichFruit > 20) thatFruit=watermelon;
etc.
Instantiate(thatFruit, spawnPoint, Quaternion.identity);
}
Something like that should work. Just assign a gameObject where you want the spawn point to be and your fruits as gameObjects. In game you just have to call the spawnFruit function.
It didn't really work. This is my script and I want it so in 5 seconds it spawns a fruit every second. It creates a random number every second. I am having trouble spawning the fruit too. Is there something in my script I can fix? Thank you in advance.
using UnityEngine; using System.Collections;
public class spawnFruit : $$anonymous$$onoBehaviour {
public GameObject spawnPoint;
public GameObject watermelon;
public GameObject strawberry;
public GameObject grape;
public GameObject banana;
public GameObject orange;
public GameObject tomato;
public GameObject pineapple;
public GameObject apple;
public GameObject blueberries;
public GameObject raspberries;
public GameObject pear;
public GameObject blackberries;
public GameObject lemon;
public int number = 0;
void Start()
{
InvokeRepeating("number", 5, 1);
number = Random.Range (1,87);
}
void spawn_fruit()
{
if (number >= 1)
if (12 >= number)
{
Instantiate(watermelon, Vector2(0.18,4.79), Quaternion.identity);
}
}
}
What do you do with invokeRepeating? Do you mean: "InvokeRepeating("spawn_fruit", 5, 1);"?
in your spawn_fruit void, you are saying that if number is 1 or larger (so every possible number which makes it not relevant) then look if number is smaller then 12. Just write if (number > 0) then ..., (number > 11) then ..., (number > 21) then ..., and go ahead like that. $$anonymous$$uch easier if you ask me.
Also, if your script does not work. Never ever ever say "it does not work" without describing WHAT EXACTLY does not work. What happens on screen? If you press play? Are there Errors or Warnings?
I hope that this will work but I have a parsing error at line 118 and I don't know how to fix it. Besides that, will this code work possibly? Thank you for helping me out.
using UnityEngine; using System.Collections;
public class spawnFruit : $$anonymous$$onoBehaviour {
public GameObject spawnPoint;
public GameObject watermelon;
public GameObject strawberry;
public GameObject grape;
public GameObject banana;
public GameObject orange;
public GameObject tomato;
public GameObject pineapple;
public GameObject apple;
public GameObject blueberries;
public GameObject raspberries;
public GameObject pear;
public GameObject blackberries;
public GameObject lemon;
public int number = 0;
void Start()
{
InvokeRepeating("spawn_fruit", 5, 1);
number = Random.Range (1,87);
}
void spawn_fruit()
{
if (number > 0)
{
Instantiate(watermelon, Vector2(0.18,4.79), Quaternion.identity);
}
if (number > 12)
{
Instantiate(strawberry, Vector2(0.18,4.79), Quaternion.identity);
}
if (number > 23)
{
Instantiate(grape, Vector2(0.18,4.79), Quaternion.identity);
}
if (number > 33)
{
Instantiate(banana, Vector2(0.18,4.79), Quaternion.identity);
}
if (number > 42)
{
Instantiate(orange, Vector2(0.18,4.79), Quaternion.identity);
}
if (number > 50)
{
Instantiate(tomato, Vector2(0.18,4.79), Quaternion.identity);
}
if (number > 58)
{
Instantiate(pineapple, Vector2(0.18,4.79), Quaternion.identity);
}
if (number > 65)
{
Instantiate(apple, Vector2(0.18,4.79), Quaternion.identity);
}
if (number > 71)
{
Instantiate(blueberries, Vector2(0.18,4.79), Quaternion.identity);
}
if (number > 76)
{
Instantiate(raspberries, Vector2(0.18,4.79), Quaternion.identity);
}
if (number > 80)
{
Instantiate(pear, Vector2(0.18,4.79), Quaternion.identity);
}
if (number > 83)
{
Instantiate(blackberries, Vector2(0.18,4.79), Quaternion.identity);
}
else if (number > 85)
{
Instantiate(lemon, Vector2(0.18,4.79), Quaternion.identity);
}
}
Your missing the last }.
Every { needs a } somewhere in the script, otherwise you will get a parsing error. You opened the void spawn_fruit but didnt close it at the end.