[Help] How can I instantiate game objects across a field while some objects are "more rare" than others?
I currently have two working scripts. One which spreads a randomized number of collectible gameobjects across an a given field, and one which gives a probability of being instantiated to 6 levels of rarity of similar objects. The first script looks like this:
using UnityEngine;
using System.Collections;
public class CollectibleDistributor1 : MonoBehaviour {
public GameObject collectible;
public static int spawnNum = Random.Range (75, 215);
void spawn(){
for(int i = 0; i < spawnNum; i++)
{
Vector3 collects = new Vector3(this.transform.position.x + Random.Range(-245f, 245f),
this.transform.position.y + Random.Range(-150f,150f));
Instantiate(collectible, collects, Quaternion.identity);
}
}
// Use this for initialization
void Start () {
spawn();
}
}
The second script looks like this:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class NewCollectibleDistributor : MonoBehaviour {
public Spawn06[] dots;
public static int spawnNum = Random.Range (75,215);
void Start()
{
SpawnObjects ();
}
void Update()
{
}
void SpawnObjects()
{
int i = Random.Range (0, 100);
Vector3 collects = new Vector3(this.transform.position.x + Random.Range(-245f, 245f),
this.transform.position.y + Random.Range(-150f,150f));
for (int j = 0; j < dots.Length; j++) {
if (i >= dots[j].minProbRange && i <= dots[j].maxProbRange){
Instantiate (dots[j].dot, collects, Quaternion.identity);
}
}
}
}
[System.Serializable]
public class Spawn06
{
public GameObject dot;
public int minProbRange = 0;
public int maxProbRange = 0;
}
I've poked around the instantiate Unity Docs as well as watched multiple videos and googled this question but couldn't really piece together anyway to get these two scripts to work together to create a field of randomly placed objects of 6 levels of rarity. I feel like there is a very simple solution, though I'm new to coding and simply don't know how to get it work.
I'm also curious as to if it would be possible to maybe make a class (?, again, new to coding) out of this information, then procedurally generate a handful of these maps together, making outer most ones contain more rare objects, unless it's easier to simply expand the parameters of the field. This is an entirely different question I know. Any extra info is also appreciated, I'd like to learn all I can! Thanks in advance! :)
Answer by tormentoarmagedoom · Aug 08, 2018 at 01:21 PM
Good day.
Fee things:
The word this, refears to this script, and you acces this.transform which is not correct, because the transform is from the gameobject co taining the script, not the script itself, so you shoud ise gameobject.transform or simply transform.
Second: you only need to make some if sentences with a random number, if the if becomes true, enter in a Instantiate function, so depending on numbers you will instantiate more or less.
You should be able to do the script if understant what you have in ypjr cose...
Bye!