Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

46 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Parent object to instantiate 1 Answer

Why is it that some sprites render in game view and others do not? 1 Answer

How to instantiate a static 2D object facing the player 0 Answers

I want to create a game like stage 05 of Circus Charlie (an NES game) but i don't know where to start 0 Answers

Player object not instantiating at where I want it to 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges