- Home /
Creating a random number of spawning items on collision
I'm trying to create a C# script that allows me to spawn a random amount of 'Money' when the 'Person' object collides with an object. I've tested some stuff but really i'm just clueless. Heres my code
The script that walks to the desk and detects collision
public class deskWalk : MonoBehaviour
{
public Transform tr;
public Rigidbody rb;
public float speed = 2f;
public Vector3 direction;
public bool stopped;
public Transform BankDesk;
public GameObject MoneySpawner;
// Update is called once per frame
void Start()
{
int randNum;
randNum = Random.Range(0, 10);
Debug.Log("Moving Now");
stopped = false;
tr.Rotate (0, 180, 0 * Time.deltaTime);
}
private void FixedUpdate()
{
if (stopped)
{
direction = Vector3.zero;
}
else
{
rb.AddForce(tr.forward * speed * Time.deltaTime);
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.transform.name == "Cube")
{
Debug.Log("Lol im stopped");
stopped = true;
MoneySpawner.GetComponent<moneySpawner>().spawn();
}
}
public void leave()
{
if (stopped == true)
{
stopped = false;
Debug.Log("Leaving Now!");
tr.Rotate(0, 90, 0 * speed * Time.deltaTime);
rb.AddForce(tr.forward * speed * Time.deltaTime);
}
}
}
The script that spawns the money object
public class moneySpawner : MonoBehaviour
{
public GameObject money;
public void spawn()
{
moneyPooler.Instantiate(money, transform.position, Quaternion.identity);
}
}
I would recommend cache that moneySpawner script in the start method, you don't want to use GetComponent more than you have to, it can be costly on performance. e.g.
public moneySpawner moneySpawnerScript;
moneySpawnerScript = $$anonymous$$oneySpawner.GetComponent<moneySpawner>();
//then use moneySpawnerScript.spawn() ins$$anonymous$$d
however, if you only have one moneyspawner script in your scene, it might be worth creating a static method to spawn, so you only ever need to use 'moneySpawner.spawn()' when you want to spawn money
Answer by losingisfun · Apr 05, 2018 at 01:06 AM
try using a for loop and a random range, like:
int roll = Random.Range(0,10);
for (int I = 0; I < roll; I++) {
//instanstiate something here
}
Yes, finally lol
Code worked perfectly, I tend to get either 1 or 7 quite often after testing it a few times which may be really lucky or unlucky, but I think I need to tweak the code a bit to receive a more 'random' number.
Thank you very much for your help!
Answer by Armadillq · Apr 05, 2018 at 01:16 AM
For anyone who would like the code that ended up working for me
public class moneySpawner : MonoBehaviour
{
public GameObject money;
public void spawn()
{
int randNum = Random.Range(0, 10);
for (int I = 0; I < randNum; I++)
{
moneyPooler.Instantiate(money, transform.position, Quaternion.identity);
}
}
}
Your answer
Follow this Question
Related Questions
Random Spawning of Objects at Random Locations 1 Answer
Using Vector3 in ViewportToWOrldPoint 1 Answer
Enemies spawn on top of each other(C#)(Unity) 1 Answer
How do I make a game object spawn several times in semi-random locations? 1 Answer
How to make enemy prefab spawn at random times between 1.0f to 10.0f. 1 Answer