Question by
abhishek7 · Apr 17, 2016 at 01:43 PM ·
instantiateplatformer
Instantiating multiple prefabs? Why?
I have a basic vertical platformer, in which when the player lands on the first platform, another latform is randomly generated, and so on. Score increases with each platform landed on. However, when the player lands on a platform, multiple platforms are instantiated. Why? Here's my variables:
public int collid = 0;
bool what = true;
public bool mover = false;
public GameObject pause;
public GameObject Menu;
public GameObject Restart;
public Text Score2;
public Text myText;
public GameObject Score3;
public float jump;
public float movespeed;
public float moveactual;
public bool grounded=true;
public bool finish=false;
public int score = 0;
public GameObject prefab2;
public GameObject prefab3;
public int orglev = 0;
public int jumpcount = 0;
public Transform prefab;
public bool gen;
public GameObject prefab1;
public RigidbodyInterpolation2D interpolation;
public float platform = 0;
and my actual code:
void OnTriggerEnter2D(Collider2D other){
if (other.tag == "MovingPlatform") {
mover = true;
}
if (other.tag == "Hills" || other.tag =="MovingPlatform") {
grounded = true;
if (other.GetInstanceID () != collid) {
score++;
platform++;
Vector3 hillpos = other.transform.position;
gen = true;
float rand = 0;
while (-1<=rand && rand<=1) {
rand = Random.Range (-3, 3);
}
float xpos = hillpos.x + rand;
float ypos = hillpos.y + 3;
if (score==platform) {
if (score <= 10) {
Instantiate (prefab1, new Vector3 (xpos, ypos, 0), Quaternion.identity);
}
if (score > 10 && score<20) {
float dec = Random.Range (1, 10);
if (mover) {
Instantiate (prefab1, new Vector3 (xpos, ypos, 0), Quaternion.identity);
}
if(mover==false){
if (dec <= 3) {
if (mover == false) {
Instantiate (prefab2, new Vector3 (xpos, ypos, 0), Quaternion.identity);
}
} else {
Instantiate (prefab1, new Vector3 (xpos, ypos, 0), Quaternion.identity);
}
}
}
if (score >= 20) {
float dec1 = Random.Range (1, 10);
if (mover) {
Instantiate (prefab1, new Vector3 (xpos, ypos, 0), Quaternion.identity);
}
if(mover==false){
if (dec1 <= 2) {
Instantiate (prefab3, new Vector3 (xpos, ypos, 0), Quaternion.identity);
}
if (dec1 >= 3 && dec1 <= 5) {
Instantiate (prefab2, new Vector3 (xpos, ypos, 0), Quaternion.identity);
}
if (dec1 > 5) {
Instantiate (prefab1, new Vector3 (xpos, ypos, 0), Quaternion.identity);
}
}
}
mover = false;
}
collid = other.GetInstanceID ();
}
}
The above is the relevant code, but just in case, here is my entire code for the player:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Player : MonoBehaviour {
public int collid = 0;
bool what = true;
public bool mover = false;
public GameObject pause;
public GameObject Menu;
public GameObject Restart;
public Text Score2;
public Text myText;
public GameObject Score3;
public float jump;
public float movespeed;
public float moveactual;
public bool grounded=true;
public bool finish=false;
public int score = 0;
public GameObject prefab2;
public GameObject prefab3;
public int orglev = 0;
public int jumpcount = 0;
public Transform prefab;
public bool gen;
public GameObject prefab1;
public RigidbodyInterpolation2D interpolation;
public float platform = 0;
void Start () {
}
void Update () {
myText.text = "Score:" + score;
Score2.text = "Score:" + score;
if (grounded) {
if (Input.GetKeyDown (KeyCode.Space)||Input.GetKeyDown(KeyCode.UpArrow)||Input.touchCount==1) {
GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, jump);
}
}
if (transform.position.y < -20) {
orglev = 0;
int oldscore = PlayerPrefs.GetInt ("highscore", 0);
if (score > oldscore) {
PlayerPrefs.SetInt ("highscore", score);
}
PlayerPrefs.Save ();
/*Score3.SetActive (true);
Menu.SetActive (true);
pause.SetActive (false);*/
Application.LoadLevel (Application.loadedLevelName);
}
moveactual = 0;
if (Input.GetKey (KeyCode.D)||Input.GetKey(KeyCode.RightArrow)) {
moveactual=movespeed;
}
if (Input.GetKey (KeyCode.A)||Input.GetKey(KeyCode.LeftArrow)) {
moveactual=-movespeed;
}
GetComponent<AudioSource>().Play();
GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveactual, GetComponent<Rigidbody2D> ().velocity.y);
//Input.acceleration.x*movespeed
}
void OnTriggerEnter2D(Collider2D other){
if (other.tag == "MovingPlatform") {
mover = true;
}
if (other.tag == "Hills" || other.tag =="MovingPlatform") {
grounded = true;
if (other.GetInstanceID () != collid) {
score++;
platform++;
Vector3 hillpos = other.transform.position;
gen = true;
float rand = 0;
while (-1<=rand && rand<=1) {
rand = Random.Range (-3, 3);
}
float xpos = hillpos.x + rand;
float ypos = hillpos.y + 3;
if (score==platform) {
if (score <= 10) {
Instantiate (prefab1, new Vector3 (xpos, ypos, 0), Quaternion.identity);
}
if (score > 10 && score<20) {
float dec = Random.Range (1, 10);
if (mover) {
Instantiate (prefab1, new Vector3 (xpos, ypos, 0), Quaternion.identity);
}
if(mover==false){
if (dec <= 3) {
if (mover == false) {
Instantiate (prefab2, new Vector3 (xpos, ypos, 0), Quaternion.identity);
}
} else {
Instantiate (prefab1, new Vector3 (xpos, ypos, 0), Quaternion.identity);
}
}
}
if (score >= 20) {
float dec1 = Random.Range (1, 10);
if (mover) {
Instantiate (prefab1, new Vector3 (xpos, ypos, 0), Quaternion.identity);
}
if(mover==false){
if (dec1 <= 2) {
Instantiate (prefab3, new Vector3 (xpos, ypos, 0), Quaternion.identity);
}
if (dec1 >= 3 && dec1 <= 5) {
Instantiate (prefab2, new Vector3 (xpos, ypos, 0), Quaternion.identity);
}
if (dec1 > 5) {
Instantiate (prefab1, new Vector3 (xpos, ypos, 0), Quaternion.identity);
}
}
}
mover = false;
}
collid = other.GetInstanceID ();
}
}
if (other.tag == "Finish") {
finish = true;
orglev = orglev + 1;
}
if (other.tag == "Base") {
grounded = true;
int oldscore = PlayerPrefs.GetInt ("highscore", 0);
if (score > oldscore) {
PlayerPrefs.SetInt ("highscore", score);
}
PlayerPrefs.Save ();
if (score>0) {
/*Score3.SetActive (true);
Menu.SetActive (true);
Restart.SetActive (true);
pause.SetActive (false);*/
Application.LoadLevel (Application.loadedLevelName);
}
}
if (other.tag == "Coin") {
Destroy (other.gameObject);
score++;
Debug.Log(score);
}
}
void OnTriggerExit2D(Collider2D other){
grounded = false;
gen = false;
if (other.tag == "MovingPlatform") {
}
if (other.tag == "Hills" || other.tag == "MovingPlatform" && this.transform.position.y > other.transform.position.y){
//
//score++;
//Destroy (other.gameObject);
}
"Hills" is the tag for a platform, "MovingPlatform" is tag for a moving platform. Any help would be seriously appreciated.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612075018im_/https://answers.unity.com/themes/thub/images/avi.jpg)